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
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
sliding window
hash maps
two pointers
strings
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported