Reported August 2020
Arcesiumbinary search

K-th Character in an Infinite String

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

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

Arcesium reportedly put this one in front of candidates in August 2020, and the constraint is the whole story: k goes up to 10^16, so building the string is dead on arrival. You can't append blocks until you hit position k. The pattern is block-skipping with a closed-form length, then a small index calculation inside one block. If you've got an OA coming, this is a math-plus-binary-search problem dressed up as a string problem. StealthCoder is the safety net on the live OA if the indexing falls apart under pressure, but the idea is short enough to hold in your head.

The problem

Build an infinite string from non-empty strings s and t in numbered blocks:
Append s once.
Append t twice.
Append s three times.
Append t four times.
Continue alternating strings while increasing the repetition count by one for each block.
Given a 1-indexed position k, return the character at that position.

Function
kthInfiniteCharacter(s: String, t: String, k: long) → char

Examples
Example 1
s = "a"
t = "bc"
k = 4
return = b
The beginning is abcbcaaabcbcbcbc.... Position 4 contains b.
Example 2
s = "ab"
t = "x"
k = 7
return = a
The first three blocks are ab, xx, and ababab. Position 7 is the third character of the third block, which is a.

Constraints
1 <= s.length, t.length <= 100.
1 <= k <= 10^16.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Block i has repeat count i and uses s when i is odd, t when i is even. Its length is i * len(s) or i * len(t). Total length after n blocks splits into odd and even sums: odd blocks give len(s) times the sum of odd numbers, even blocks give len(t) times the sum of even numbers. Both have closed forms, so total(n) is O(1). Binary search the smallest n where total(n) >= k. Since k is up to 10^16, n is roughly 10^8 at most, so the search takes about 30 steps. Subtract total(n-1) from k to get the offset inside block n. Then take offset-1 modulo the length of the block's string and index into it. The classic pitfalls are off-by-one on the 1-indexed k, overflow in 32-bit ints, and mixing up which string goes with odd blocks. Use 64-bit everywhere. If you blank on the sum formulas, StealthCoder can cover you during the live OA.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill K-th Character in an Infinite String 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Arcesium reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

K-th Character in an Infinite String FAQ

What's the trick in the Arcesium K-th character problem?+

Never build the string. Compute the total length after n blocks with closed-form sums, binary search for the block containing k, then use modulo to find the exact character inside that block's repeated string.

Why can't brute force work here?+

k can reach 10^16. Block lengths grow with the repetition count, so generating characters until position k would take far too long and too much memory. You need to skip whole blocks arithmetically instead of walking through them.

What formulas do I need for the total length?+

For n blocks, odd-numbered blocks contribute len(s) times the sum of odd indices up to n, and even-numbered blocks contribute len(t) times the sum of even indices. Count k1 = ceil(n/2) odd blocks and k2 = floor(n/2) even blocks. The odd sum is k1 squared. The even sum is k2 times (k2+1).

Where do people usually get this wrong?+

Three spots. Overflow from using 32-bit ints, off-by-one with the 1-indexed k, and swapping s and t for odd versus even blocks. Test with the example where s is ab, t is x, and k is 7. The answer should be a.

How do I prepare for this in 48 hours?+

Practice binary search on a monotonic function, where you search over an answer instead of an array. Then hand-trace both examples from the problem, writing out block lengths and cumulative totals. That covers nearly everything this question tests.

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

OA at Arcesium?
Invisible during screen share
Get it