Longest Conveyor Segment With At Most K Distinct Item Types

Last updated: November 29, 2026

Quick Overview

Items on a conveyor belt are labeled by type. A packing station can handle at most k distinct types at once. Return the length of the longest contiguous segment of the belt containing at most k distinct item types.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Items on a conveyor belt are labeled by type. A packing station can handle at most k distinct types at once. Return the length of the longest contiguous segment of the belt containing at most k distinct item types.

A sliding window question in the style of Amazon technical screens, grounded in pack station constraints. The interviewer checks for a variable-size window with a count map, correct shrinking logic, and a linear time argument.

What the Interviewer Expects
  • Use two pointers with a frequency map
  • Shrink from the left while distinct count exceeds k
  • Delete keys when their count hits zero
  • Argue O(n) because each pointer moves at most n times
  • Handle k = 0 and empty input
Key Topics to Cover
Sliding Window
Two Pointers
Hash Map
Arrays
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 return the segment itself, or all maximal segments?
  • What if the belt is an unbounded stream and you need the answer so far?
  • How would you handle exactly k distinct types instead of at most k?
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

Belt is a list of hashable labels, possibly empty. k >= 0. Return an int length; 0 if k is 0 or the belt is empty....

High-Level Design

Expand the right pointer, incrementing the label's count. While the map has more than k keys, decrement the count of the label at the left pointer, removing the key when it reaches zero, and move left...


Submit Your Answer
Markdown supported