← Trexquant Interview Insights

Trexquant·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Interviewed for a Software Engineer role at Trexquant and got hit with a streaming median problem. Pretty classic heap question but the pressure of getting the rebalancing logic right on the spot is no joke.

Questions Asked (1)

Q1

Design a data structure that accepts integers one at a time from a stream and can return the current median at any point, with O(log n) insertion and O(1) median retrieval.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was to just keep a sorted list and binary search insert, which is technically O(log n) to find the position but O(n) to shift elements.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: median definition for even/odd counts, data types, and whether the stream can be replayed. Then propose a two-heap solution (max-heap for lower half, min-heap for upper half) with balanced sizes, explaining how insertion maintains balance in O(log n) and median retrieval is O(1).

Pro tip: Mention that the two-heap approach is the standard optimal solution, but also discuss edge cases like empty stream, duplicates, and negative numbers to show thoroughness. If asked about trade-offs, note that a balanced BST or order-statistic tree also works but is more complex to implement.

1. Clarify requirements and constraints

Ask about median definition (average of two middle elements for even count), input size, memory limits, and whether the data structure needs to support deletions or only insertions.

2. Propose the two-heap approach

Explain using a max-heap for the lower half and a min-heap for the upper half. The heaps are kept balanced such that their sizes differ by at most 1.

3. Detail insertion logic

Describe how to insert a new number: add to one heap, then rebalance by moving the top element to the other heap if size difference exceeds 1. This ensures O(log n) time.

4. Detail 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) time.

5. Discuss complexity and alternatives

State time complexities: O(log n) insertion, O(1) median. Mention alternative structures like balanced BST with order statistics, but note the two-heap solution is simpler and efficient.

Key Points to Mention

  • Two-heap strategy: max-heap for lower half, min-heap for upper half.
  • Balancing condition: sizes differ by at most 1, with lower heap having >= elements when odd.
  • Insertion: push to appropriate heap, then rebalance by moving top element if needed.
  • Median retrieval: O(1) by peeking at heap tops.
  • Time complexity: O(log n) insertion due to heap operations, O(1) median.
  • Edge cases: empty stream, even/odd number of elements, duplicates, negative numbers.

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