Implement nested object path lookup
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates parsing and traversal of nested maps and arrays, robust error handling for missing keys and out-of-bounds indices, and reasoning about algorithmic time and space complexity.
Constraints
- Map keys do not contain dots or brackets
Examples
Input: ({'a': {'b': [10, 20]}}, 'a.b[1]')
Expected Output: 20
Explanation: Prompt example.
Input: ({'a': {'b': [10, 20]}}, 'a.b[3]')
Expected Output: None
Explanation: Out-of-range index.
Input: ({'ab': [{'c': 5}]}, 'ab[0].c')
Expected Output: 5
Explanation: Object inside array.
Input: ({'a': 1}, 'a.b')
Expected Output: None
Explanation: Type mismatch.
Hints
- Tokenize the path, then walk the current object by token type.