Parse password from indexed matrix file
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates file parsing, matrix indexing with a specified coordinate origin, handling multiple indexed blocks, and string assembly into an ordered password, testing competencies in file I/O, coordinate transformations, and robust input handling.
Constraints
- Input is well-formed per the format.
- Index lines match: [x,y] with 0-based non-negative integers and optional spaces.
- Each block's matrix has 1 to 1000 rows; all rows have the same length (1 to 1000).
- 0 <= x < number_of_columns, 0 <= y < number_of_rows for each block.
- Blocks within a password are separated by at least one empty line after each matrix.
- Passwords are separated by a line that is exactly '---'. Only the first password group must be processed.
- Total characters across the first password group <= 200,000.
Examples
Input: [1,2] abcd efgh ijkl [0,0] XYZ PQR --- [0,1] ab cd
Expected Output: Pb
Explanation: First group has two blocks. Block [1,2] over abcd/efgh/ijkl (origin lower-left, y up) -> top row column 1 -> 'b'; block [0,0] over XYZ/PQR -> bottom-left -> 'P'. Prepend in block-encounter order: start 'b', then prepend 'P' -> 'Pb'. The '---' group and everything after it is ignored.
Input: [0,0] A [2,1] 012 345
Expected Output: 2A
Explanation: Block [0,0] over single-cell 'A' -> 'A'; block [2,1] over 012/345 -> y=1 is the top row '012', x=2 -> '2'. Prepend -> '2A'. Group ends at EOF (no '---').
Input: [0,0] Z
Expected Output: Z
Explanation: Edge: single block, single-cell matrix. (0,0) on the lower-left origin is the only cell -> 'Z'. EOF terminates the matrix and the first group.
Input: [ 1 , 0 ] QRS TUV
Expected Output: U
Explanation: Edge: index line carries optional spaces. x=1,y=0 -> y=0 is the bottom row 'TUV', column 1 -> 'U'.
Input: [2,0] ..... ##X## ..... [0,2] LMN OPQ RST
Expected Output: L.
Explanation: Edge: two blocks, no '---' (group ends at EOF). Block [2,0]: y=0 is the bottom row '.....', column 2 -> '.'. Block [0,2]: y=2 is the top row 'LMN', column 0 -> 'L'. Prepend in order: start '.', then prepend 'L' -> 'L.'.
Input: [1,2]\nabcd\nefgh\nijkl\n\n[0,0]\nXYZ\nPQR\n\n---\n[0,1]\nab\ncd\n
Expected Output: Pb
Explanation: Robustness: identical first group to test 1 but delivered with double-escaped literal backslash-n line breaks. The solution decodes them back to real newlines and still yields 'Pb'.
Hints
- Parse the index line with a regex like ^\[\s*(\d+)\s*,\s*(\d+)\s*\]$.
- Remember the origin is at the lower-left: matrix row index = (height - 1 - y).
- Collect characters per block in encounter order and prepend; an efficient way is to append and reverse at the end.