Parse a password from a matrix
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in grid traversal, sequence-driven state tracking, string manipulation, and boundary/obstacle validation while requiring time and space complexity reasoning.
Constraints
- 1 <= m, n (the matrix is non-empty and rectangular)
- Each cell of matrix is a single character; '#' denotes a blocked cell.
- moves consists only of the characters 'U', 'D', 'L', 'R' (any other character is treated as invalid and yields "ERROR").
- 0 <= len(moves) <= 10^5
- Return "ERROR" if a move leaves the grid or enters a blocked cell, or if the starting cell is out of bounds or blocked.
Examples
Input: ([['a','b','c'],['d','e','f'],['g','h','i']], 0, 0, 'RRDD')
Expected Output: 'abcfi'
Explanation: Path: (0,0)a -> (0,1)b -> (0,2)c -> (1,2)f -> (2,2)i. All characters differ from the previous, so the password is 'abcfi'.
Input: ([['a','a','b'],['c','d','e']], 0, 0, 'RR')
Expected Output: 'ab'
Explanation: Path: (0,0)a -> (0,1)a -> (0,2)b. The second 'a' matches the previously appended 'a', so it is skipped, giving 'ab'.
Input: ([['a','b'],['#','d']], 0, 0, 'D')
Expected Output: 'ERROR'
Explanation: Moving Down from (0,0) lands on (1,0) which is blocked ('#'), so the result is 'ERROR'.
Input: ([['a','b'],['c','d']], 0, 0, 'U')
Expected Output: 'ERROR'
Explanation: Moving Up from row 0 leaves the grid, so the result is 'ERROR'.
Input: ([['x']], 0, 0, '')
Expected Output: 'x'
Explanation: A single-cell grid with no moves: only the starting character 'x' is appended.
Input: ([['a','b','c']], 0, 0, 'RRLL')
Expected Output: 'abcba'
Explanation: Path: a -> b -> c -> b -> a. No two consecutive visited characters are equal, so every character is appended: 'abcba'.
Input: ([['#','b'],['c','d']], 0, 0, 'R')
Expected Output: 'ERROR'
Explanation: The starting cell (0,0) is blocked ('#'), so the result is 'ERROR' before any move is processed.
Input: ([['a','a','a'],['a','a','a']], 0, 0, 'RRDLL')
Expected Output: 'a'
Explanation: Every visited cell holds 'a', so after the first append all subsequent identical characters are skipped, collapsing to 'a'.
Input: ([['a','b'],['c','d']], 0, 0, 'DRUL')
Expected Output: 'acdba'
Explanation: Path loops around: (0,0)a -> (1,0)c -> (1,1)d -> (0,1)b -> (0,0)a. All consecutive characters differ, giving 'acdba'.
Hints
- Track two things as you walk: your current (row, col) position and the last character you appended. A new character is only appended when it differs from that last-appended character.
- Map each move letter to a (dr, dc) delta. Before committing to a move, compute the candidate cell and validate it: it must be inside the grid AND not a '#'. Fail fast with "ERROR" the moment a move is invalid.
- Don't forget the starting cell itself — it must be valid (in-bounds and not blocked), and its character seeds the password before you process any moves.