Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Partition Labels
Problem
You are given a string s of lowercase letters. Cut it into as many contiguous pieces as possible such that no letter appears in more than one piece. Return the list of piece lengths, in order from left to right.
shas between 1 and 20,000 characters, all lowercase English letters.- The pieces, concatenated in order, must reproduce
sexactly.
Examples
s = "gtgbgsbahahjjkfkflf"→[7, 4, 2, 6]. Pieces"gtgbgsb","ahah","jj","kfkflf"; every letter lives entirely in one piece, and no finer cut works.s = "zaaza"→[5]. The letterzsits at both ends, so the string cannot be cut at all.s = "xyz"→[1, 1, 1]. All letters distinct: each is its own piece.
Hints
- If a piece contains the first
a, it must also contain the lasta. What does that tell you about where the piece must end? - As you scan, keep track of the furthest position the current piece is obliged to reach. When is it safe to cut?
- Precompute each letter's last index; sweep left to right maintaining
end = max(end, last[ch]), and cut wheni == end.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.