Longest Inbox Stretch Containing At Most K Distinct Senders
Last updated: November 29, 2026
Quick Overview
Given the sequence of sender addresses for messages in an Outlook inbox ordered by arrival and an integer k, return the length of the longest contiguous stretch that involves at most k distinct senders.
Microsoft
November 29, 20260
0
0 solved
Given the sequence of sender addresses for messages in an Outlook inbox ordered by arrival and an integer k, return the length of the longest contiguous stretch that involves at most k distinct senders.
A sliding window question in the style of Microsoft technical screens, framed around Outlook conversation grouping and Focused Inbox heuristics. The interviewer checks that you maintain window state with a frequency map and shrink correctly, keeping the solution linear.
What the Interviewer Expects
- Identify the problem as a variable-size sliding window
- Track sender counts inside the window with a dictionary
- Shrink from the left until the distinct count is at most k
- Delete zero-count keys so len(map) equals distinct count
- Prove O(n) by counting pointer moves
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 also return the start index of the best stretch?
- What if k changes per query and you get many queries?
- How would you handle a stream where you only keep the last hour of mail?
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 senders of strings and integer k >= 0. Output: maximum length j - i + 1 such that senders[i..j] has at most k distinct values. If k is 0 the answer is 0. The list can be long (a la...
High-Level Design
Use two pointers left and right over a window plus a Counter of senders currently inside it. Extend right one message at a time and increment that sender's count. If the number of keys exceeds...