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

0

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
sliding window
two pointers
frequency map
amortized analysis
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported