← Cognitiv Interview Insights

Cognitiv·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Cognitiv ML Engineer interview had a classic streaming median problem with a follow-up that actually made me think harder than the main question. Pretty algorithmic for an ML role, but not surprising given the company.

Questions Asked (2)

Q1

Design a data structure that accepts integers one at a time and returns the current median after each insertion. Implement add(x) and getMedian() operations, and analyze the time complexity of your solution.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Two heaps, one max-heap for the lower half and one min-heap for the upper half.

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) to maintain the median in O(log n) per insertion and O(1) for getMedian. Explain the balancing logic and how to handle even/odd total counts. Analyze time and space complexity, and discuss trade-offs versus other approaches like sorted arrays or balanced BSTs.

Pro tip: Mention that this two-heap pattern is a classic streaming median solution and is directly applicable to online learning scenarios where data arrives sequentially. Also, note that Python's heapq is a min-heap, so you can simulate a max-heap by negating values.

1. Clarify requirements and constraints

Confirm that add(x) can be called multiple times and getMedian() may be called after each insertion. Ask about expected data volume and whether duplicates are allowed.

2. Propose the two-heap approach

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

3. Detail the insertion algorithm

Outline the steps: add to max-heap, move the largest from max-heap to min-heap, then if min-heap is larger, move the smallest back to max-heap. This ensures all elements in max-heap are <= all elements in min-heap.

4. Explain median retrieval

If total count is odd, the median is the top of the larger heap; if even, it's the average of the tops of both heaps.

5. Analyze complexity and trade-offs

State that add(x) takes O(log n) time due to heap operations, getMedian() takes O(1), and space is O(n). Compare with alternatives like sorted list (O(n) insertion) or balanced BST (O(log n) but more complex).

Key Points to Mention

  • Two heaps: max-heap for lower half, min-heap for upper half
  • Balancing condition: sizes differ by at most 1, and all elements in max-heap <= all in min-heap
  • Insertion steps: push to max-heap, move max to min-heap, rebalance if needed
  • Median calculation: odd count -> top of larger heap; even count -> average of tops
  • Time complexity: O(log n) for add, O(1) for getMedian; space O(n)
  • Trade-offs: two-heap is optimal for streaming median; alternatives like sorted array or BST have worse or comparable complexity with more overhead

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

Q2

Follow-up: if the full input is available upfront and you want to minimize auxiliary memory usage, how would you find the median without the extra space overhead of the two-heap approach?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where it got more interesting.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that with the full input available upfront, you can use a selection algorithm like Quickselect to find the median in O(n) average time with O(1) auxiliary space, or sort the array in-place if O(n log n) time is acceptable. Emphasize that this avoids the O(n) auxiliary space of the two-heap approach while still achieving linear expected time.

Pro tip: Mention that Quickselect can be made deterministic with the Median of Medians algorithm for O(n) worst-case time, but in practice, randomized Quickselect is often preferred for its simplicity and speed. Also, note that for ML applications, if the data is already sorted or nearly sorted, a simple in-place sort might be more efficient.

1. Clarify constraints and requirements

Confirm that the full input is available in memory and that auxiliary space should be minimized. Discuss whether the input can be modified (in-place) and whether worst-case or average-case time complexity is more important.

2. Introduce Quickselect

Describe the Quickselect algorithm: partition the array around a pivot, then recursively search only the side containing the median. This yields O(n) average time and O(1) auxiliary space.

3. Address pivot selection and worst-case

Explain that random pivot selection gives expected O(n) time, while Median of Medians guarantees O(n) worst-case time. Mention trade-offs: randomized is simpler and faster in practice, deterministic has higher constant factors.

4. Compare with sorting

Note that sorting the array in-place (e.g., heapsort) also uses O(1) auxiliary space but takes O(n log n) time. For median finding, Quickselect is asymptotically faster on average.

5. Relate to ML context

Connect to ML scenarios: median is used for robust statistics, outlier detection, or quantile computation. Emphasize that memory efficiency matters for large datasets, and in-place algorithms are valuable when data fits in memory but auxiliary space is limited.

Key Points to Mention

  • Quickselect algorithm: partition-based selection with O(n) average time and O(1) auxiliary space.
  • Randomized pivot selection for expected linear time; Median of Medians for guaranteed linear time.
  • In-place sorting (e.g., heapsort) as an alternative with O(n log n) time and O(1) space.
  • Trade-offs: Quickselect modifies the input array; if input must be preserved, a copy is needed (but that defeats space savings).
  • Handling duplicates and edge cases (even number of elements, all elements equal).
  • Applicability to ML: median for robust statistics, quantile normalization, and memory-constrained environments.

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