← Meta Interview Insights

Meta·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Meta ML engineer interview with a pretty meaty algorithms question about sliding window medians. One round, heavy on complexity analysis and edge case handling. Left feeling okay about the approach but not totally confident on the even-k median definition.

Questions Asked (1)

Q1

Given an array and an integer k, return the median of every contiguous subarray of length k. Your solution should run in O(n log k) time, handle duplicates and deletions correctly, and you need to define what median means when k is even. Also analyze space complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is the kind of question where the basic idea clicks fast but the details wreck you.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the median definition for even k, then propose a sliding window with two heaps (max-heap for lower half, min-heap for upper half) to maintain the median in O(log k) per element. Discuss how to handle deletions using lazy deletion or indexed heaps, and analyze time and space complexity.

Pro tip: Explicitly state the median convention for even k (e.g., average of two middle elements) and mention that lazy deletion can cause heap size drift, so periodic cleanup or a balanced approach is needed to keep operations O(log k).

1. Clarify requirements and edge cases

Define median for even k (e.g., average of two middle values) and discuss handling duplicates, deletions, and edge cases like k > n or k = 1.

2. Choose data structures

Select two heaps (max-heap for lower half, min-heap for upper half) to maintain the median, and decide on a deletion strategy (lazy deletion with counters or indexed heaps).

3. Design sliding window algorithm

Initialize the heaps with the first k elements, balance them, compute the median, then slide the window by adding the new element and removing the old one, rebalancing after each operation.

4. Analyze complexity

Explain that each insertion and deletion takes O(log k) time, leading to O(n log k) total time, and that space is O(k) for the heaps and any auxiliary structures.

5. Discuss trade-offs and alternatives

Mention alternative approaches like order-statistic trees or Fenwick trees, and compare their complexity and implementation difficulty to the two-heap method.

Key Points to Mention

  • Median definition for even k (average of two middle elements)
  • Two-heap approach: max-heap for lower half, min-heap for upper half
  • Handling deletions via lazy deletion or indexed heaps
  • Time complexity: O(n log k) due to O(log k) insert/delete per element
  • Space complexity: O(k) for the heaps and auxiliary structures
  • Edge cases: k > n, k = 1, duplicates, and negative numbers

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.