Reported August 2023
Arcesiumtwo pointers

Reverse First K Characters in Every 2K Block

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

Strip away the wording and this Arcesium OA, reported in August 2023, is a stride loop with a reverse inside it. Jump through the string in steps of 2k, and at each step flip the first k characters. That's the whole thing. The edge cases are the only place people lose points, and they come from the leftover tail at the end of the string. It's the two-pointers pattern in its simplest form. If your head goes blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the clean version. Otherwise, read this and you probably won't need it.

The problem

Given a string s and a positive integer k, process s from left to right in consecutive blocks of 2 * k characters.
For every complete block of 2 * k characters, reverse its first k characters and leave its next k characters unchanged.
If fewer than k characters remain, reverse all remaining characters.
If at least k but fewer than 2 * k characters remain, reverse the first k remaining characters and leave the rest unchanged.
Return the transformed string.

Function
reverseFirstKInEvery2KBlock(s: String, k: int) → String

Examples
Example 1
s = "abcdefgh"
k = 2
return = "bacdfegh"
In abcd, reverse ab to obtain bacd. In efgh, reverse ef to obtain fegh. Combining the blocks gives bacdfegh.
Example 2
s = "abcdefg"
k = 3
return = "cbadefg"
Reverse abc in the first six-character block, producing cbadef. Only g remains, so reversing the final one-character suffix leaves it unchanged.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: loop i from 0 to n in steps of 2k. For each i, reverse the slice from i to min(i + k, n) - 1. That single min() covers both tail rules. If fewer than k remain, you reverse everything left. If between k and 2k remain, you reverse only the first k. No separate branches needed. Convert the string to a char array first, since strings are immutable in most languages and slicing repeatedly costs extra. Use two pointers, left at i and right at min(i + k, n) - 1, and swap inward until they cross. Time is O(n), space is O(n) for the array. The common pitfall is stepping by k instead of 2k, or forgetting to clamp the right bound and running off the end. Trace Example 2 by hand before submitting. If the live OA freezes you, StealthCoder is the hedge that gives you the loop instantly.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Reverse First K Characters in Every 2K Block 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. If you're reading this with an OA window open, you're who this was built for.

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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Reverse First K Characters in Every 2K Block FAQ

How hard is the Arcesium reverse first K in every 2K block question really?+

Easy. It's a single loop with a swap routine. The difficulty is entirely in the tail handling, and one min() call solves that. If you can write an in-place array reverse, you can finish this in a few minutes.

What's the trick to the tail cases?+

Clamp the right edge of the reversal to min(i + k, n) - 1. When fewer than k characters remain, that reverses all of them. When between k and 2k remain, it reverses just the first k. You don't need separate if-branches for either rule.

Should I use two pointers or built-in reverse?+

Two pointers on a char array is safest. It's O(n) time, easy to explain, and avoids slicing overhead. Built-in reverse on slices works too, but swapping inward from both ends shows you understand the mechanics if anyone reviews your code.

What edge cases should I test before submitting?+

Test k larger than the string length, which reverses the whole string. Test a length exactly divisible by 2k. Test a length leaving one trailing character, like Example 2. Test k equal to 1, where nothing changes. These four cover nearly every failure.

How do I prepare for this in 48 hours?+

Write the stride loop and the swap helper from memory twice. Then trace both examples by hand, checking the final partial block. Spend the rest of your time on other string and two-pointer basics, since this one is a warm-up level problem.

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