Reverse and Append
Updated
Problem statement
You are given a string s. You can reverse a part of it in one of two ways:
- Pick an index
iand reverse the part from the start of the string up to indexi, countingi(reverse towards the left). - Pick an index
iand reverse the part from indexito the end of the string, countingi(reverse towards the right).
The rest of the string stays as it was. For example, if s = "dbaacca", picking index 1 towards the left reverses "db" and gives "bdaacca". Picking index 2 towards the left reverses "dba" and gives "abdacca". Picking index 5 towards the right reverses "ca" and gives "dbaacac".
Return every different string you can get this way, in alphabetical order. Do not include s itself, even when a reversal leaves it unchanged. If no reversal gives a new string, return an empty list.
Complete the function reverseAndAppend in the editor.
reverseAndAppend has the following parameter:
String s: the original string
The function returns a List<String>: all the different strings you can get, in alphabetical order, without s.
Function
reverseAndAppend(s: String) → List<String>Examples
Example 1
s = "dbaacca"return = ["aabdcca", "abdacca", "accaabd", "bdaacca", "caabdca", "ccaabda", "daccaab", "dbaaacc", "dbaacac", "dbaccaa"]Reversing from the start up to index i gives (for i = 1 to 6): bdaacca, abdacca, aabdcca, caabdca, ccaabda, accaabd.
Reversing from index i to the end gives (for i = 0 to 5): accaabd, daccaab, dbaccaa, dbaacca, dbaaacc, dbaacac.
Reversing from index 3 to the end gives dbaacca again, because acca reads the same backwards. That is s itself, so it is left out. accaabd shows up twice, so it is listed once.
Sorted, the 10 different strings are the output.
Constraints
1 ≤ s.length ≤ 50shas only lowercase English letters.
More Uber interview questions
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Serialize and Deserialize an N-Ary TreeONSITE INTERVIEW · Seen Oct 2026
- Find Robots by Blocker DistancesPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Next Number with Disjoint DigitsElevenLabs · OAElevenLabs interview questions
- String Permutations in Custom Character OrderWex · OA · PHONE SCREEN
- Generate ParenthesesThe Walt Disney Company · OAThe Walt Disney Company interview questions
- All Palindrome PartitionsExpedia · OAExpedia interview questions
- Generate ParenthesesAtlassian · OAAtlassian interview questions
- Equal-Sum Digit PartitionsAlchemy · OA
- Maximum Number after K SwapsIntuit · OAIntuit interview questions
- Generate Balanced ParenthesesInMobi · OAInMobi interview questions
- Work ScheduleSnowflake · OASnowflake interview questions
- Work Schedules (for MTS2)Nutanix · OANutanix interview questions