Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Turn Off Adjacent Pixels and LRU
00:00
5 left

Turn Off Adjacent Pixels and LRU

MediumPython

Problem

A Tripadvisor surface editor receives a binary pixel matrix and a sequence of taps. Each tap at (row, column) turns off that pixel and its orthogonally adjacent pixels: up, down, left, and right. Pixels outside the matrix are ignored. Apply taps in order.

In the same function, implement an LRU cache for surface metadata. Process put and get operations, returning the values produced by get in order. A put for an existing key updates its value and makes the key most recently used. When the cache exceeds capacity, evict the least recently used key.

Return the final matrix and all cache lookup results.

Formal Specification

Implement process_surface(matrix, taps, capacity, cache_operations), where matrix is a list of lists containing 0 or 1, taps is a list of [row, column] coordinates, capacity is a positive integer, and each cache operation is a dictionary such as {"op": "put", "key": "hotel", "value": 4} or {"op": "get", "key": "hotel"}. Return [final_matrix, get_results], using None for a cache miss.

Constraints

  • 1 <= rows, columns <= 1,000
  • 0 <= len(taps) <= 10^5
  • 1 <= capacity <= 10^5
  • 0 <= len(cache_operations) <= 2 * 10^5
  • Cache keys are strings and values are integers
  • Taps may repeat and may lie on matrix boundaries

Function Signature

def process_surface(matrix, taps, capacity, cache_operations):
Interviewer

Your question is Turn Off Adjacent Pixels and LRU. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.