Reverse words in a string based on Fibonacci series indices
Filed under Morgan Stanley
Free read 1 of 3Confirm if word reversal means reversing the letters inside the word or reversing the positions of the words.
The question
Given a space-separated string of words, reverse the words whose 1-based index corresponds to a non-duplicate number in the Fibonacci series.
Constraints
- 1 <= number of words <= 100
- Words contain alphanumeric characters.
Examples
- Input
- s = "the quick brown fox jumps"
- Output
- "eht quick nworb xof jumps"
- Why
- Indices 1, 2, 3, 5 are Fibonacci numbers (1, 1, 2, 3, 5 - unique non-duplicate terms: 1, 2, 3, 5). The words at those indices are reversed.
Approach
Confirm if word reversal means reversing the letters inside the word or reversing the positions of the words.
Generate Fibonacci numbers up to the word count, filter unique values, and process each word sequentially.
Split the string into a list of words. Precompute or generate a set of unique Fibonacci indices up to the length of the list. Iterate through the list and reverse characters of words whose 1-based index is in the set.
Empty string or strings with fewer than three words.
Tips
- What they are testing: Test with string lengths smaller than Fibonacci values and single-word strings.
- Common mistake: Confusing 0-based and 1-based indexing when matching against the Fibonacci sequence.
Related Morgan Stanley questions
Collected from public candidate posts and coding forums. Updated Nov 2026.