The base matrix check was fine, just compare each cell to its upper-left neighbor and you're done in O(m*n) time.
Start by defining the Toeplitz property and giving a straightforward O(mn) time, O(1) space check by comparing each cell to its top-left neighbor. Then discuss the streaming variant, where you maintain the last row and compare each new row to the previous one, highlighting the trade-offs in memory and latency.
Pro tip: For the streaming case, emphasize that you only need to keep the previous row, not the entire matrix, and that you can process each row in O(n) time. This shows you understand memory-efficient algorithms and can handle real-world constraints like limited memory or infinite streams.
Restate the Toeplitz property: for all i>0 and j>0, matrix[i][j] == matrix[i-1][j-1]. Ask about matrix dimensions, data types, and whether the matrix is stored in memory or streamed.
Iterate through the matrix starting from row 1, column 1, and compare each element to its top-left neighbor. If any mismatch, return false; otherwise true. This is O(mn) time and O(1) extra space.
Time: O(mn) because each cell is visited once. Space: O(1) for the in-memory version. Mention that you could also check diagonals explicitly, but the neighbor comparison is simpler and equally efficient.
When rows arrive one at a time, maintain only the previous row. For each new row, compare its elements (from index 1 onward) to the previous row's elements shifted by one. If any mismatch, return false. This uses O(n) space and O(n) time per row.
Mention edge cases: single row/column, empty matrix. For streaming, note that you cannot backtrack, so you must decide on the fly. Also discuss potential optimizations like early termination and handling large streams with limited memory.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Circular buffer plus a running sum, pretty standard.
Start by clarifying the problem requirements, then propose a circular buffer (ring buffer) with a running sum to achieve amortized O(1) push and query. Discuss edge cases like fewer than k elements, numerical precision, and overflow, and explain how to handle them.
Pro tip: Mention that using a running sum can accumulate floating-point error; suggest periodic recomputation or using a higher-precision accumulator (e.g., Kahan summation) to maintain accuracy.
Confirm that k is fixed, operations are push and query, and that amortized O(1) time and O(k) space are required. Ask about the data type (e.g., float, double) and expected stream rate.
Use a circular buffer of size k to store the last k values, a running sum, and a count of elements pushed so far. This allows O(1) push (overwrite oldest, update sum) and O(1) query (sum / min(count, k)).
When fewer than k elements have been pushed, return the average of all elements pushed so far (sum / count). Handle k=0 or negative k gracefully (e.g., throw exception or return 0).
Floating-point errors can accumulate in the running sum; consider periodic recomputation or Kahan summation. For integer streams, use a wider type (e.g., long long) or modular arithmetic to avoid overflow.
Confirm amortized O(1) time for push and query, and O(k) space. Discuss trade-offs: running sum is fast but less precise; recomputation is precise but O(k) per query.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Two heaps, max-heap for the lower half and min-heap for the upper half, rebalancing on each slide.
Start by clarifying the problem constraints (e.g., array size, k, data types) and then propose a solution using two heaps (max-heap for lower half, min-heap for upper half) to maintain the median in O(log k) per insertion/deletion, achieving O(n log k) overall. Discuss how to handle duplicates (heaps naturally handle them) and even-length windows (median is average of max of lower heap and min of upper heap). Also mention memory trade-offs: O(k) extra space for heaps, which is optimal.
Pro tip: Mention that you can optimize further using a balanced BST or order-statistic tree for O(n log k) but with higher constant factors; however, the two-heap approach is simpler and often faster in practice. Also, note that for very large n, you might consider a streaming approach with approximate medians if exactness is not critical.
Ask about input size, k relative to n, data types, whether the array is static or streaming, and if exact median is required. Confirm output format (list of medians).
Propose two heaps: a max-heap for the lower half and a min-heap for the upper half. Explain how to maintain balance (size difference ≤1) and how to compute median for odd/even windows.
Describe how to add a new element and remove the oldest element. For removal, use lazy deletion (mark elements as invalid) or maintain a hash map of counts to avoid O(k) removal. Discuss rebalancing after each operation.
State time complexity: O(n log k) due to heap operations. Space complexity: O(k) for heaps and optional hash map. Compare with alternatives like sorting each window (O(n k log k)) or using a balanced BST (O(n log k) but higher overhead).
Explain how duplicates are handled naturally by heaps. For even-length windows, median is average of two middle values. Discuss edge cases: k=1, k=n, empty array, and negative numbers.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.