← Trexquant Interview Insights

Trexquant·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Got a coding question at Trexquant that was squarely in data structures territory. Classic median-from-stream problem, the kind you either know cold or fumble through.

Questions Asked (1)

Q1

Design a data structure that supports adding numbers from a stream one at a time and returning the median of all numbers seen so far at any point.

Algorithms & Data StructuresSystem Design
Author's notes

The two-heap approach is the right answer here and I knew it, but I fumbled the balancing logic under pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use two heaps: a max-heap for the lower half and a min-heap for the upper half, keeping their sizes balanced. After each insertion, rebalance and compute the median from the top elements. This gives O(log n) insertion and O(1) median retrieval.

Pro tip: Explicitly discuss edge cases like empty stream and even/odd counts, and mention that this approach is optimal for streaming data because it avoids sorting on each query.

1. Clarify requirements and constraints

Ask about the expected frequency of insertions vs. median queries, memory limits, and whether the stream can be empty. This shows you consider real-world usage.

2. Propose the two-heap solution

Explain that a max-heap stores the smaller half and a min-heap stores the larger half, with size difference at most 1. This maintains the median at the tops.

3. Detail insertion and rebalancing

Describe adding to the appropriate heap, then moving the top element if sizes differ by more than 1. Emphasize O(log n) time.

4. Explain median retrieval

If heaps are equal size, median is the average of the two tops; otherwise, it's the top of the larger heap. This is O(1).

5. Analyze complexity and edge cases

State time and space complexity, and discuss handling empty stream, even/odd counts, and potential integer overflow when averaging.

Key Points to Mention

  • Two-heap approach with max-heap for lower half and min-heap for upper half
  • Balancing condition: size difference at most 1
  • O(log n) insertion and O(1) median retrieval
  • Handling even and odd number of elements
  • Edge cases: empty stream, single element, duplicate values
  • Space complexity O(n) for storing all elements

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