Reported June 2026
Amazoncounting

Get Smallest Base Segment

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reported this one in June 2026, and the name hides a neat counting problem. You get a string of missing characters and a base segment length, and you have to pick the segment that needs the fewest copies to cover everything. Brute forcing every possible segment of length segmentSize blows up fast, since there are 26^segmentSize options. You don't need to enumerate anything. It's a counting and binary search on replications problem. If you blank during the live OA, StealthCoder runs invisibly and can hand you the approach.

The problem

In Amazon's distributed storage network, some critical data segments are missing. They are represented by a string missingData. The system restores data by choosing a base segment of length segmentSize and repeatedly appending copies of that base segment to a generated string.
A base segment is valid if, after some number of replications, the generated string contains every character in missingData at least as many times as it appears in missingData.
Among all valid base segments, choose one that requires the fewest replications. If more than one base segment requires that same minimum number of replications, return the lexicographically smallest one. If no valid base segment exists, return "-1".

Function
getSmallestBaseSegment(segmentSize: int, missingData: String) → String
Complete the function getSmallestBaseSegment.
getSmallestBaseSegment has the following parameters:
int segmentSize: the length of the base segment that can be replicated
String missingData: a string representing the missing data segments
Returns String: the lexicographically smallest valid base segment of length segmentSize that minimizes the required number of replications, or "-1" if no such segment exists.

Examples
Example 1
segmentSize = 2
missingData = "aavvavv"
return = "av"
The character a appears 3 times and v appears 4 times. Both "av" and "va" require 4 replications. Since "av" is lexicographically smaller, it is returned.
Example 2
segmentSize = 1
missingData = "abc"
return = "-1"
A base segment of length 1 can contain only one distinct character, so it cannot generate all three required characters.

Constraints
1 <= segmentSize <= missingData.length
missingData consists of lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: pick a replication count k. A segment repeated k times has k copies of each character it contains. So for each letter with count c in missingData, the segment needs at least ceil(c / k) copies of it. Sum those across letters. If the sum is at most segmentSize, k works. Find the smallest k from 1 up to the max letter count. Stop at the first valid k, since validity is monotonic as k grows. If no k works, return -1, which happens when distinct letters exceed segmentSize. Then build the segment: take the required copies of each letter, and fill the leftover slots with 'a' to stay lexicographically smallest. Sort the characters at the end. Pitfall: filling leftovers with the wrong letter, or forgetting that extra slots can't be skipped because the length must equal segmentSize exactly. StealthCoder is your hedge if the sort and fill step slips under pressure.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Get Smallest Base Segment cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Get Smallest Base Segment FAQ

What's the core trick in Get Smallest Base Segment?+

Fix the replication count k, then compute how many copies of each letter the segment needs: ceil(count / k). If the total fits in segmentSize, k is valid. Find the smallest valid k, then build the sorted segment with leftover slots filled by 'a'.

How do I know when to return -1?+

Return -1 when even the largest useful k can't fit. With k equal to the max letter count, each present letter needs just one copy, so the sum equals the distinct letter count. If that exceeds segmentSize, no segment works. Example 2 shows this with 3 distinct letters and size 1.

Why fill leftover slots with 'a'?+

Once the minimum k is fixed, extra slots can hold any letter without changing the replication count. Adding 'a' gives the lexicographically smallest result after sorting. Adding it never hurts coverage, since extra letters only add more copies than required.

Can I binary search k instead of looping?+

Yes. Validity is monotonic, because a larger k never needs more slots. So binary search over 1 to the max letter count works. A linear scan is also fine at 26 letters per check, so pick whichever you can write without bugs.

How should I prepare for this in 48 hours?+

Practice string counting problems with ceiling division and constructing the lexicographically smallest answer. Write this one cold twice: count letters, find minimum k, build the sorted string. Test both examples by hand, including the aavvavv case where k equals 4 gives av.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it