Top K Escalation Reasons in a Rolling Time Window

Last updated: November 29, 2026

Quick Overview

Given escalation events (timestamp, reason) from conversations handed to human agents, return the k most frequent reasons in the window (now - window, now], breaking ties alphabetically.

Sierra
Coding & Algorithms
Software Engineer
Sierra
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given escalation events (timestamp, reason) from conversations handed to human agents, return the k most frequent reasons in the window (now - window, now], breaking ties alphabetically.

Sierra customers watch why conversations escalate to humans, since every escalation is a missed resolution. This is a short screening problem that checks counting, top-k selection, and precise window boundaries.

What the Interviewer Expects
  • Filter events to the window with correct open and closed ends
  • Count with a hash map
  • Use a heap or sort with the key (-count, reason)
  • Return fewer than k results when fewer reasons exist
  • State time complexity in terms of events and distinct reasons
Key Topics to Cover
heaps
top-k
hash counting
time windows
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you answer this query every minute on a live stream without rescanning?
  • How would you do approximate top-k with bounded memory?
  • How would you break this down per customer brand?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Window is (now - window, now], default one day in seconds. Output is a list of reason strings, most frequent first, ties alphabetical. If there are fewer than k distinct reasons, return all of them....

High-Level Design

Count reasons that fall in the window, then select the k smallest by the key (-count, reason). heapq.nsmallest keeps a heap of size k, which beats a full sort when there are many distinct reasons....


Submit Your Answer
Markdown supported

Related Questions