Bit at an Index After Repeated Binary Expansion
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The rule that 1 becomes 10 while 0 becomes 00 is the whole question in this Amazon OA, reported in September 2026. Rounds go up to 30, so the expanded string can pass 10^9 characters per original bit. Building it is dead on arrival. You need the bit-manipulation shortcut that maps the index back to its source character. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the approach in real time. Here's the pattern so you don't need it.
The problem
Start with a binary string bits. In one expansion round, replace every character independently: 0 becomes 00. 1 becomes 10. After exactly rounds expansions, return the bit at the zero-based position index. The position is guaranteed to exist in the expanded string. Function expandedBit(bits: String, rounds: int, index: int) → int Examples Example 1 bits = "01" rounds = 1 index = 2 return = 1 One expansion produces 0010, whose zero-based index 2 contains 1. Example 2 bits = "1" rounds = 2 index = 3 return = 0 The two expansions are 1 -> 10 -> 1000, and its last bit is 0. Example 3 bits = "101" rounds = 0 index = 2 return = 1 With zero rounds, query the original string directly. Constraints 1 <= bits.length <= 10^5 bits contains only 0 and 1. 0 <= rounds <= 30 0 <= index <= 10^9 index < bits.length * 2^rounds.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Every original character expands into a block of exactly 2^rounds characters. So the source character is bits[index >> rounds], and the offset inside the block is index & (2^rounds - 1). Now look at the block. A 0 becomes all zeros, because 00 stays 00 forever. A 1 becomes 1 followed by zeros: 1, 10, 1000, and so on. So the answer is 1 only if the source bit is 1 and the offset is 0. Otherwise it's 0. That's O(1) after parsing. The common pitfall is simulating the expansion, or recursing round by round. Another trap is using int for 2^rounds. 2^30 fits, but be careful with shifts elsewhere. Check example 2: index 3 with rounds 2 gives offset 3, so the answer is 0. StealthCoder is the hedge if the shift logic slips under pressure in the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Bit at an Index After Repeated Binary Expansion 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Bit at an Index After Repeated Binary Expansion FAQ
What's the trick in the Amazon repeated binary expansion question?+
Never build the string. Each original bit becomes a block of 2^rounds characters. Find the block with index >> rounds, then the offset with index & (2^rounds - 1). The answer is 1 only when the source bit is 1 and the offset is 0. Otherwise it's 0.
How hard is this problem really?+
Easy once you see the block structure, but it looks scary because of the huge expanded length. The code is about three lines. The difficulty is resisting the urge to simulate. Rounds up to 30 and index up to 10^9 are the signal that simulation fails.
Why does a 1 expand to 1 followed by zeros?+
Each round turns 1 into 10, and the 1 in front stays the leading character. The trailing 0 becomes 00, then 0000, and so on. After r rounds you get a 1 followed by 2^r - 1 zeros. A 0 only ever produces zeros.
What edge cases should I test before submitting?+
Test rounds = 0, where the offset is always 0 and you return the original bit. Test index 0 on a 1, which returns 1. Test the last index of a block, which returns 0. Also test the maximum rounds of 30 to confirm your shift doesn't overflow.
How do I prepare for this in 48 hours?+
Practice mapping a flat index to a group and offset using shifts and masks. Work a few small cases by hand, like the examples here. Then write the O(1) solution from memory twice. That covers this question and similar index-mapping problems that show up in assessments.