Implement matrix transpose and KV store
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates array manipulation and algorithmic reasoning via an n x n matrix transpose and object-oriented design, data structure competency, and time-complexity reasoning for a key-value store API.
Part 1: Transpose an n x n Matrix
Constraints
- 0 <= n <= 200
- -10^9 <= matrix[i][j] <= 10^9
- The input matrix is guaranteed to be square
Examples
Input: []
Expected Output: []
Explanation: An empty matrix stays empty after transposition.
Input: [[42]]
Expected Output: [[42]]
Explanation: A 1 x 1 matrix is unchanged.
Input: [[1, 2], [3, 4]]
Expected Output: [[1, 3], [2, 4]]
Explanation: The first column becomes the first row, and the second column becomes the second row.
Input: [[0, -1, 2], [7, 5, 9], [3, 8, 6]]
Expected Output: [[0, 7, 3], [-1, 5, 8], [2, 9, 6]]
Explanation: Each column of the original becomes a row in the result.
Hints
- Figure out where matrix[i][j] should appear in the output.
- Create an empty n x n result matrix, then fill result[j][i] = matrix[i][j].
Part 2: Key-Value Store with set, get, and setAll
Constraints
- 0 <= len(queries) <= 200000
- Keys are strings of length 1 to 50
- -10^9 <= value <= 10^9
- An efficient solution should avoid touching every existing key during setAll
Examples
Input: []
Expected Output: []
Explanation: No operations means no get results.
Input: [("set", "x", 10), ("get", "x")]
Expected Output: [10]
Explanation: After setting x to 10, getting x returns 10.
Input: [("set", "a", 1), ("set", "b", 2), ("setAll", 7), ("get", "a"), ("get", "b")]
Expected Output: [7, 7]
Explanation: Both existing keys are updated logically by setAll.
Input: [("get", "missing"), ("setAll", 5), ("get", "missing")]
Expected Output: [None, None]
Explanation: A missing key stays missing, even if setAll is called when no such key exists.
Input: [("set", "a", 1), ("set", "b", 2), ("setAll", 5), ("set", "b", 8), ("get", "a"), ("get", "b")]
Expected Output: [5, 8]
Explanation: Key a keeps the setAll value, while b is overwritten by a later set.
Hints
- A naive setAll that updates every stored key is too slow when there are many keys.
- Store a timestamp for each key's latest set, and compare it with the timestamp of the most recent setAll.