Member-only story
Seemingly Easy Problems That Prove Tough in Interviews
2 min readNov 7, 2024
You will be given a string which represents a sentence. Write a golang program that rearranges the words in the given sentence in such a way like all words with odd length come first followed by words with an even length. Maintain the original order of words with the same length
input := "This is a test sentence with some words"
output := "This with some test is a sentence words"
// "This" "with" "some" are odd length, followed by the even length words in their original order.
Constraints:
- The input sentence will contain only lowercase letters and spaces.
- There will be a single space between each word.
Why This Problem Is Tricky:
- It seems deceptively simple: At first look, it may appear like a basic string manipulation problem.
- It requires careful planning: You need to come up with a strategy to separate and reorder the words based on their length while preserving the original order within each group.