Count Items Between Compartment Delimiters on a Warehouse Shelf
Last updated: November 29, 2026
Quick Overview
A shelf is described by a string of '*' (item) and '|' (compartment wall). For each query (start, end), inclusive and 0-indexed, count the items that sit inside fully closed compartments within that substring. Answer up to 100,000 queries on a shelf of up to 100,000 characters efficiently.
Amazon
November 29, 20260
0
0 solved
A shelf is described by a string of '*' (item) and '|' (compartment wall). For each query (start, end), inclusive and 0-indexed, count the items that sit inside fully closed compartments within that substring. Answer up to 100,000 queries on a shelf of up to 100,000 characters efficiently.
This is a classic in the style of Amazon Online Assessments for fulfillment roles. Brute force per query times out, so graders look for precomputed prefix counts plus nearest-wall arrays that make each query O(1).
What the Interviewer Expects
- Identify that only items between the first and last wall inside the range count
- Precompute prefix item counts
- Precompute next wall to the right and previous wall to the left for each index
- Answer each query in O(1)
- Handle ranges with zero or one wall
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 support updates that add or remove a wall?
- What if queries arrive online and the shelf string is streamed?
- How would you return the count per compartment instead of a total?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Shelf string s of length n, queries as a list of (start, end) with 0 <= start <= end < n. Output a list of ints. Items before the first wall or after the last wall in the range are not enclosed and do...
High-Level Design
For a query, find L = first '|' at or after start, and R = last '|' at or before end. If L >= R there is no closed compartment, answer 0. Otherwise the answer is the number of '*' strictly between L a...