Reorder a string by half the character
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Reordering a string by half usually means splitting the string into two halves and then combining them in a new order. The first thing to settle is the exact rule, because "reorder by half" can mean interleaving two halves, swapping them, or treating odd-length strings in a special way.
Define the transformation clearly
One common interpretation is to split the string into two halves and interleave the characters:
- input:
abcdefgh - halves:
abcdandefgh - result:
aebfcgdh
For odd-length strings, you also need a rule for the extra character. A simple choice is to keep the extra character in the first half.
A straightforward Python implementation
The function below uses that exact rule:
Output:
This solution is simple and works in linear time because it visits each character once.
Why the midpoint matters
The midpoint decides how odd-length strings behave. In the example above, (len(text) + 1) // 2 gives the first half the extra character.
If you change the split to len(text) // 2, the second half gets the extra character instead, and the output order changes. That is why the problem statement should always define the odd-length rule before implementation starts.
An index-based version
If you want the logic written more explicitly, you can construct the result by index:
This version avoids temporary slices, though in normal Python code the first version is often easier to read.
Complexity and testing
The interleaving algorithm runs in O(n) time and uses O(n) extra space for the result buffer. That is usually the right tradeoff because Python strings are immutable, so building the result in a list and joining once is faster than repeated string concatenation.
It is also worth testing a few edge cases:
- the empty string
- a one-character string
- even-length input
- odd-length input
Those cases confirm whether your midpoint rule matches the intended output. They also protect you from off-by-one mistakes, which are the most common bug in half-splitting string code.
Alternative interpretation: swap the halves
Sometimes the real requirement is not interleaving at all. It is only:
- input:
abcdefgh - output:
efghabcd
That is a different operation:
Because the phrase is ambiguous, it is worth confirming the expected output with one example before you code the algorithm.
Common Pitfalls
- Starting to code before defining what "reorder by half" means for odd-length strings.
- Confusing interleaving two halves with simply swapping the halves.
- Writing index logic that goes out of bounds on the shorter half.
- Building strings by repeated concatenation in a loop instead of collecting characters and joining once.
Summary
- Reordering by half usually starts with splitting the string into two halves and defining a clear recombination rule.
- Interleaving the halves is a common interpretation and runs in linear time.
- Odd-length strings need an explicit midpoint rule.
- Confirm the expected transformation with concrete examples before implementing it.
Related reading
- Reorder vector using a vector of indices
- Reordering a list to maximize difference of adjacent elements
- Reordering of array elements
- Repeatedly removing the maximum average subarray
- Replace list of list with condensed list of list while maintaining order
- Replace wildcards in a binary string avoiding three identical consecutive letters
- Replacing the Linux Kernel's Page Replacement Algorithm
- Representing and solving a maze given an image

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.