Design top-K frequency store for varying workloads

Quick Overview

This question evaluates understanding of data structures, algorithmic complexity, and workload-driven design trade-offs for maintaining frequency counts and computing top-K results in an online stream.

Design top-K frequency store for varying workloads

Company: Microsoft

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

### Scenario You need to design an in-memory component that tracks how often each key appears in a stream of events. The component must support these operations: - `record(key)`: increment the count for `key` by 1 (insert `key` if not present). - `topK(k)`: return the `k` keys with the highest counts so far (order among them does not matter). Assume: - There can be up to `N` distinct keys. - Operations happen online as the stream arrives. ### Task Describe how you would implement this component under two different workload patterns: 1. **Read-heavy**: there are many more `topK(k)` calls than `record(key)` calls. 2. **Write-heavy**: there are many more `record(key)` calls than `topK(k)` calls. For each case, specify: - The main data structures you would use. - Time complexity of `record` and `topK`. - Space complexity. - Why your design is appropriate for that workload (the trade-offs you’re making).

Quick Answer: This question evaluates understanding of data structures, algorithmic complexity, and workload-driven design trade-offs for maintaining frequency counts and computing top-K results in an online stream.

|Home/Software Engineering Fundamentals/Microsoft
Microsoft logo
Microsoft
Nov 3, 2025, 12:00 AM
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
4
0

Scenario

You need to design an in-memory component that tracks how often each key appears in a stream of events.

The component must support these operations:

  • record(key) : increment the count for key by 1 (insert key if not present).
  • topK(k) : return the k keys with the highest counts so far (order among them does not matter).

Assume:

  • There can be up to N distinct keys.
  • Operations happen online as the stream arrives.

Task

Describe how you would implement this component under two different workload patterns:

  1. Read-heavy : there are many more topK(k) calls than record(key) calls.
  2. Write-heavy : there are many more record(key) calls than topK(k) calls.

For each case, specify:

  • The main data structures you would use.
  • Time complexity of record and topK .
  • Space complexity.
  • Why your design is appropriate for that workload (the trade-offs you’re making).
Loading comments...