Solve max-subarray, min-unique, and grid-jump
Company: Cisco
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates array algorithms and algorithmic problem-solving skills, specifically contiguous-sum optimization (greedy/DP reasoning), frequency counting and memory-constrained selection for unique minima, and grid traversal simulation with boundary and visit-state management.
Constraints
- 1 <= m, n
- Coordinates are 1-based with (1,1) at top-left
- Only landing cells are marked visited; the jumped-over cell is not marked
- Stop when all four directions from the current cell are invalid
- Recommended: m * n <= 200000 for Python solutions
Solution
from typing import List
def last_grid_jump_cell(m: int, n: int) -> List[int]:
# Directions: right, up, left, down (counterclockwise order)
dirs = [(0, 1), (-1, 0), (0, -1), (1, 0)]
r, c = 1, 1
visited = {(r, c)} # store visited landing cells
dir_idx = 0 # start facing right
while True:
attempts = 0
moved = False
while attempts < 4:
dr, dc = dirs[dir_idx]
nr, nc = r + 2 * dr, c + 2 * dc
if 1 <= nr <= m and 1 <= nc <= n and (nr, nc) not in visited:
r, c = nr, nc
visited.add((r, c))
moved = True
break # keep the same direction after a successful move
else:
dir_idx = (dir_idx + 1) % 4 # rotate 90° counterclockwise
attempts += 1
if not moved:
return [r, c]
Explanation
Time complexity: O(m*n) in the worst case (each cell is considered at most once as a landing, with up to four direction checks per step).. Space complexity: O(m*n) in the worst case to store visited landing cells..
Hints
- Track visited landing cells; the intermediate cell is irrelevant to validity checks.
- Maintain a direction index for [right, up, left, down] and rotate counterclockwise only when a move is invalid.
- Terminate when you have attempted (and failed) all four directions from the current position without moving.