Implement Sliding Windows and LRU Cache
Company: Bytedance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates algorithmic problem-solving and data-structure design, testing sliding-window and 2D submatrix reasoning for maximizing contiguous ones with bounded flips and the design of an LRU cache that enforces O(1) get/put semantics.
Part 1: Longest Subarray of Ones
Constraints
- 0 <= len(nums) <= 200000
- nums[i] is either 0 or 1
- 0 <= k <= len(nums)
Examples
Input: ([1, 1, 0, 0, 1, 1, 1, 0, 1], 2)
Expected Output: 7
Explanation: The longest window with at most two zeros has length 7.
Input: ([], 3)
Expected Output: 0
Explanation: An empty array has no subarrays.
Input: ([0], 1)
Expected Output: 1
Explanation: The single zero can be flipped.
Input: ([1, 1, 1, 1], 0)
Expected Output: 4
Explanation: The whole array is already all ones.
Input: ([0, 0, 0], 0)
Expected Output: 0
Explanation: No flips are allowed, so no non-empty all-ones subarray exists.
Hints
- Use a sliding window and track how many zeros are currently inside the window.
- If the window contains more than k zeros, move the left pointer right until the window becomes valid again.
Part 2: Largest All-Ones Rectangular Submatrix with Up to k Flips
Constraints
- 0 <= number of rows <= 150
- 0 <= number of columns <= 150
- All rows in grid have the same length
- grid[r][c] is either 0 or 1
- 0 <= k <= rows * columns
Examples
Input: ([[1, 0, 1], [1, 0, 1], [1, 1, 1]], 1)
Expected Output: 6
Explanation: The bottom two rows across all three columns contain exactly one zero, so area 6 is achievable.
Input: ([[1, 0, 0, 1, 1]], 1)
Expected Output: 3
Explanation: In a single row, the problem reduces to the 1D version. The best width with at most one zero is 3.
Input: ([[1, 1], [1, 1]], 0)
Expected Output: 4
Explanation: The whole matrix is already all ones.
Input: ([], 3)
Expected Output: 0
Explanation: An empty grid has no valid rectangle.
Input: ([[0, 0], [0, 0]], 2)
Expected Output: 2
Explanation: You can flip at most two cells, so the largest all-ones rectangle has area 2.
Hints
- Fix a top row and a bottom row. For that row band, each column contributes how many zeros it contains between those two rows.
- Once you compress a row band into column zero-counts, the problem becomes finding the longest contiguous column range whose sum is at most k.
Part 3: LRU Cache Simulation
Constraints
- 1 <= capacity <= 100000
- 0 <= len(operations) <= 200000
- For a put, the operation format is [1, key, value]
- For a get, the operation format is [0, key]
- Keys and values fit in 32-bit signed integers
Examples
Input: (2, [[1, 1, 1], [1, 2, 2], [0, 1], [1, 3, 3], [0, 2], [1, 4, 4], [0, 1], [0, 3], [0, 4]])
Expected Output: [1, -1, -1, 3, 4]
Explanation: This is the standard LRU behavior: keys are evicted in least-recently-used order.
Input: (1, [[1, 2, 1], [0, 2], [1, 3, 2], [0, 2], [0, 3]])
Expected Output: [1, -1, 2]
Explanation: With capacity 1, inserting key 3 evicts key 2.
Input: (3, [])
Expected Output: []
Explanation: No operations means no get results.
Input: (2, [[1, 1, 10], [1, 2, 20], [1, 1, 15], [1, 3, 30], [0, 1], [0, 2], [0, 3]])
Expected Output: [15, -1, 30]
Explanation: Updating key 1 makes it most recent, so key 2 is evicted when key 3 is inserted.
Hints
- A hash map gives O(1) lookup by key, but you still need O(1) updates to recency order.
- A doubly linked list lets you move a node to the front and remove the least recently used node from the back in O(1).