Reverse First K Characters in Every 2K Block
Updated
Problem statement
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 * kcharacters, reverse its firstkcharacters and leave its nextkcharacters unchanged. - If fewer than
kcharacters remain, reverse all remaining characters. - If at least
kbut fewer than2 * kcharacters remain, reverse the firstkremaining characters and leave the rest unchanged.
Return the transformed string.
Function
reverseFirstKInEvery2KBlock(s: String, k: int) → StringExamples
Example 1
s = "abcdefgh"k = 2return = "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 = 3return = "cbadefg"Reverse abc in the first six-character block, producing cbadef. Only g remains, so reversing the final one-character suffix leaves it unchanged.
More Arcesium Online Assessment questions
- City Infection NumberOA · Seen Jul 2025
- Minimum Tunnel Crossing TimeOA · Seen Jul 2025
- Product of Subset MaximaOA · Seen Jul 2025
- Reconstruct the Root StreamOA · Seen Jul 2025
- Add DigitsOA · Seen Aug 2023
- Binary Tree Maximum Path SumOA · Seen Aug 2023
- Detect a Cycle in Directed Hate RelationshipsOA · Seen Aug 2023
- Robo Data Sharing DirectionOA · Seen Jul 2023
- Capital Gains Tax from TradesOA · Seen Feb 2021
- K-th Character in an Infinite StringOA · Seen Aug 2020
Similar questions at other companies
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Alternate String EndsCapital One · OACapital One interview questions
- Concatenate Digit-wise SumsZipRecruiter · OAZipRecruiter interview questions
- Format TextTikTok · OATikTok interview questions
- Thresholded Run-Length EncodingBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- REST API: Highest International StudentsRippling · OARippling interview questions
- Filter Isolated Interior Signal BitsSEDEMAC · OA