Solve matrix diagonal and sliding-window statistics
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
1) Given an m x n integer matrix, determine whether every top-left to bottom-right diagonal has the same value (Toeplitz property). Return true/false, analyze time and space complexity, and describe how to handle a streaming input of rows.
2) Design a data structure that maintains the moving average of the last k numbers in a real-time stream. Support push(x) and query() in amortized O(
1) time and O(k) space. Address numerical precision, overflow, and behavior when the stream has fewer than k elements.
3) Given an array nums and a window size k, output the median of each sliding window across the array. Achieve O(n log k) time or better. Explain the data structures you would use, how you handle duplicates and even k, and discuss memory trade-offs.
Quick Answer: Solve matrix diagonal and sliding-window statistics evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Solution
# Solution Alignment
The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.
## Problem Restatement
1) Given an m x n integer matrix, determine whether every top-left to bottom-right diagonal has the same value (Toeplitz property). Return true/false, analyze time and space complexity, and describe how to handle a streaming input of rows. 2) Design a data structure that maintains the moving average of the last k numbers in a real-time stream. Support push(x) and query() in amortized O( 1) time and O(k) space. Address numerical precision, overflow, and behavior when the stream has fewer than k elements. 3) Given an array nums and a window size k, output the median of each sliding window across the array. Achieve O(n log k) time or better. Explain the data structures you would use, how you ha...
## Recommended Approach
Represent each cell as a state. Use BFS for minimum-distance propagation, DFS with memoization for longest monotonic paths, and careful boundary checks for simulation. Store obstacles or visited cells in sets when the grid is sparse.
## Correctness
The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.
## Complexity
A full grid traversal is O(mn) time and O(mn) space in the worst case. Sparse simulation is O(number_of_commands) plus obstacle storage.
## Edge Cases and Tests
Empty grid, one row/column, blocked start or target, boundaries, repeated visits, and tie-breaking among equal-distance cells.