Top K Bing Queries With Alphabetical Tie Breaking
Last updated: November 29, 2026
Quick Overview
Given a list of query strings from a Bing log and an integer k, return the k most frequent queries, ordered by descending frequency and then alphabetically for ties.
Microsoft
November 29, 20260
0
0 solved
Given a list of query strings from a Bing log and an integer k, return the k most frequent queries, ordered by descending frequency and then alphabetically for ties.
A frequent Microsoft screen problem framed around Bing trending searches. Beyond counting, the interviewer checks your handling of the tie-break rule and whether you can pick between full sort, a size-k heap, and bucket sort with clear complexity reasoning.
What the Interviewer Expects
- Count with a hash map
- Encode the ordering rule correctly (negative count, then string)
- Use a heap of size k or heapq.nsmallest for O(n log k)
- Compare against bucket sort
- Mention streaming and approximate methods for huge logs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you do this when the log does not fit on one machine?
- What approximate structure would you use for a real-time trending list?
- How would you return results for a sliding one-hour window?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of query strings and k with 1 <= k <= number of distinct queries. Output: list of k queries. Higher counts first; equal counts in lexicographic ascending order. Queries are case-sensitiv...
High-Level Design
Count frequencies with a Counter. The ordering key for each query is (-count, query): smaller keys are better. heapq.nsmallest(k, ...) keeps a heap of size k internally, which is O(m log k) for ...