FastPrepCache Shared Image Pipeline Prefixes

Cache Shared Image Pipeline Prefixes

Anthropic logoAnthropic● HardFULLTIMEONSITE INTERVIEW
Learn

Problem statement

An image is a rectangular matrix of grayscale pixel values. Several requested transformation pipelines all start from the same source image. Each pipeline is an ordered list of transform names.

Execute the batch while caching every intermediate image by its complete transform prefix. If two pipelines share a prefix, compute that intermediate image only once. Supported transforms are:

  • ROTATE_CW: rotate the image 90 degrees clockwise.
  • FLIP_HORIZONTAL: reverse every row.
  • INVERT: replace each pixel x with 255 - x.
  • ENLARGE_2X: replace each pixel by a 2 by 2 block of that value.
  • SHRINK_2X: replace each non-overlapping 2 by 2 block with the floor of its four-value average.

Return a string report. The first string is steps=k, where k is the number of distinct non-empty prefixes actually computed. Each remaining string encodes one final image in pipeline order: comma-separate pixels within a row and separate rows with /. An empty pipeline returns the unchanged source image and adds no step.

Function

runCachedImagePipelines(image: int[][], pipelines: String[][]) → String[]

Examples

Example 1

image = [[1,2],[3,4]]pipelines = [["ROTATE_CW","INVERT"],["ROTATE_CW","FLIP_HORIZONTAL"],["ROTATE_CW","INVERT"]]return = ["steps=3","252,254/251,253","1,3/2,4","252,254/251,253"]

The shared rotation, rotation-plus-inversion, and rotation-plus-flip are the three distinct non-empty prefixes. The repeated first pipeline reuses its cached final image.

Example 2

image = [[0,255]]pipelines = [[],["INVERT"],["INVERT","INVERT"]]return = ["steps=2","0,255","255,0","0,255"]

The empty pipeline uses the source directly. The two inversion prefixes are each computed once.

Example 3

image = [[10,20],[30,40]]pipelines = [["ENLARGE_2X","SHRINK_2X"],["ENLARGE_2X"]]return = ["steps=2","10,20/30,40","10,10,20,20/10,10,20,20/30,30,40,40/30,30,40,40"]

Both requests share the enlarged image. Shrinking that nearest-neighbor enlargement reconstructs the source.

Constraints

  • 1 <= image.length, image[i].length <= 40.
  • The image is rectangular, and every pixel is between 0 and 255.
  • 0 <= pipelines.length <= 1000, and each pipeline contains at most 20 transforms.
  • Every SHRINK_2X receives an image with even positive height and width.
  • Every intermediate image has at most 160000 pixels.

More Anthropic problems

See Anthropic hiring insights
public String[] runCachedImagePipelines(int[][] image, String[][] pipelines) {
  // write your code here
}
image[[1,2],[3,4]]
pipelines[["ROTATE_CW","INVERT"],["ROTATE_CW","FLIP_HORIZONTAL"],["ROTATE_CW","INVERT"]]
expected["steps=3", "252,254/251,253", "1,3/2,4", "252,254/251,253"]
Checking account…