FastPrepInvert or Blur an Image
Problems/Invert or Blur an Image

Invert or Blur an Image

OpenAI logoOpenAI● EasyFULLTIMENEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source supplies vertical inversion, eight-neighbor blur, rounding, and isolated-pixel behavior; the blur selector and size bounds are completed for practice. The judged core task is about a 95% match.

A grayscale image is represented by a rectangular integer matrix image. Each entry is a pixel value from 0 through 255.

Apply the operation selected by filter and return a new matrix:

  • If filter == 1, reflect every row across the image's vertical axis.
  • If filter == 2, blur each pixel using the original image. Replace it with the arithmetic mean of all existing neighboring pixels among the eight surrounding positions, rounded to the nearest integer. Because values are nonnegative, a fractional part of exactly 0.5 rounds up.

When a pixel has no neighbors, keep its original value. Do not use already blurred values while computing another pixel.

Function

applyImageFilter(image: int[][], filter: int) → int[][]

Examples

Example 1

image = [[1,2,3],[4,5,6]]filter = 1return = [[3,2,1],[6,5,4]]

Reflecting across the vertical axis reverses each row.

Example 2

image = [[0,0,0],[0,255,0],[0,0,0]]filter = 2return = [[85,51,85],[51,0,51],[85,51,85]]

Each corner sees three neighbors, each edge-center sees five, and the center sees eight. Every average uses the original image and rounds to the nearest integer.

Constraints

  • 1 <= image.length <= 200
  • 1 <= image[r].length <= 200
  • All rows have the same length.
  • 0 <= image[r][c] <= 255
  • filter is 1 or 2.

More OpenAI problems

  • Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public int[][] applyImageFilter(int[][] image, int filter) {
        // write your code here
    }
}
Checking account…