Longest Log Window With At Most K Distinct Error Codes
Last updated: November 29, 2026
Quick Overview
Given a sequence of error codes emitted by pods in an OpenShift cluster and an integer k, return the length and start index of the longest contiguous window containing at most k distinct codes.
IBM
November 29, 20260
0
0 solved
Given a sequence of error codes emitted by pods in an OpenShift cluster and an integer k, return the length and start index of the longest contiguous window containing at most k distinct codes.
Red Hat OpenShift and IBM Instana teams deal with noisy log streams, and a sliding-window question on log data is a natural technical-screen fit. The interviewer watches whether you maintain counts incrementally and shrink the window correctly rather than recomputing the distinct set.
What the Interviewer Expects
- Use a two-pointer sliding window with a count map
- Shrink from the left only while distinct count exceeds k
- Track both length and start index of the best window
- Explain why each index enters and leaves the window once
- Handle k equal to zero
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 answer this for a live stream where you only see each code once?
- What if you need the window with exactly k distinct codes?
- How would you return all maximal windows instead of one?
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 codes and integer k. Output: (length, start) of the longest contiguous slice with at most k distinct values; break ties by the earliest start. This mirrors finding the longest stre...
High-Level Design
Maintain a window [left, right] and a dictionary of counts for codes inside it. Extend right one step at a time. Whenever the dictionary holds more than k keys, advance left, decrementing coun...