FastPrepImplement Circular Padding
Problems/Implement Circular Padding

Implement Circular Padding

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names circular padding without specifying tensor rank, axis order, widths, interfaces or examples. FastPrep uses a rectangular 2D matrix with symmetric row and column padding and explicit wrap rules. Approximate source match: 65%.

Implement circular padding for a matrix.

For this exercise, assume a rectangular 2D integer matrix. Add rowPadding rows above and below it and columnPadding columns on its left and right. Fill every added position by wrapping periodically across the original rows and columns. Padding may exceed an original dimension. Return the padded matrix.

At output position (r, c), read original position ((r - rowPadding) mod rows, (c - columnPadding) mod columns), using nonnegative modulo results.

Function

circularPad(matrix: int[][], rowPadding: int, columnPadding: int) → int[][]

Examples

Example 1

matrix = [[1,2],[3,4]]rowPadding = 1columnPadding = 1return = [[4,3,4,3],[2,1,2,1],[4,3,4,3],[2,1,2,1]]

One wrapped row and column are added on each side. The top-left output value is 4, and the middle 2 by 2 block remains [[1,2],[3,4]].

Example 2

matrix = [[7]]rowPadding = 1columnPadding = 2return = [[7,7,7,7,7],[7,7,7,7,7],[7,7,7,7,7]]

The one source value repeats in every output position. The output has three rows and five columns.

Example 3

matrix = [[1,2,3]]rowPadding = 0columnPadding = 0return = [[1,2,3]]

With zero padding in both directions, the output is the original one-row matrix.

Constraints

  • The matrix has between 1 and 30 rows.
  • Each matrix row has between 1 and 30 values.
  • All matrix rows have the same length.
  • Every matrix value is between -1000000000 and 1000000000.
  • rowPadding and columnPadding are integers from 0 through 30.

More Apple problems

  • Evaluate DivisionPHONE SCREEN · Seen Nov 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
  • Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
All Apple interview questionsSee Apple hiring insights
Saved
public int[][] circularPad(int[][] matrix, int rowPadding, int columnPadding) {
    // Write your solution here.
}
Checking account…