Find Obfuscate Message
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're looking at an Amazon OA from November 2024 that asks you to obfuscate a message by right-rotating exactly one substring to make the result lexicographically smallest. This is a substring selection problem disguised as a rotation problem. You pick one contiguous slice of the input, rotate it right by one position (last character moves to front), and choose the slice that produces the smallest overall string. StealthCoder will feed you the pattern and candidate solutions if you blank on the rotation logic during the live assessment.
The problem
Amazon developers are creating a new security module for Amazon Web Services. One of its functions is to obfuscate incoming messages. The obfuscation method is to select some substring of the message and right rotate that substring by 1 position one time only. The substring is selected in such a way that the resulting obfuscated message is alphabetically the smallest message that can be formed using this operation. Here, right rotating any string by 1 position is to shift every character of string a in the right direction by 1 position and remove the last character and append it to the beginning of string a. For example, a = "ahssd", after right rotation by 1 position string a changes to "dahss". Given the input message as a string, find the obfuscated message. Note: A string x is alphabetically smaller than string y, if either x is a prefix of y (and x ≠ y), or there exists such i (0 ≤ i x|, |y|)), that x[i] y[i], and for any j (0 ≤ j i) x[j] = y[j]. Here, |a| denotes the length of the string a. Function Description Complete the function findObfuscateMessage in the editor below. findObfuscateMessage has the following parameter: string message: the input message Returns string: the encrypted message Image source will be attached in next update...
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you're not optimizing a single rotation; you're trying all possible substrings, applying the right-rotate operation to each, and comparing the results lexicographically. A naive approach generates O(n^2) candidates, each comparison O(n), so worst case is O(n^3). The pattern is substring enumeration with local transformation and greedy selection of the minimum. Most candidates overthink the rotation operation itself (it's just move last char to front). The real work is iterating all substring boundaries and tracking which rotation gives the lexicographically smallest string. If you blank on the algorithm during the OA, StealthCoder will show you the enumeration framework and the comparison logic.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Find Obfuscate Message cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Obfuscate Message FAQ
What exactly is a right rotation by 1 position?+
Take the last character and move it to the front. Example: 'ahssd' becomes 'dahss'. You do this to one substring at a time, nowhere else in the string changes. The rest of the original message stays in place.
Do I have to rotate the entire string, or can I pick any substring?+
You pick any substring, from any start index to any end index. You apply the rotation to only that slice. The goal is to choose the substring and rotation that makes the entire resulting message lexicographically smallest.
How do I know which substring to rotate?+
You don't know in advance. Try all O(n^2) possible substrings. For each one, apply the right rotation, compare the full resulting string lexicographically, and keep track of the minimum. Return the string corresponding to the substring that gave the best result.
Can I skip rotating and just return the original string?+
No. The problem states you must select some substring and right rotate it 'one time only.' You cannot choose to do nothing. Every valid answer has exactly one rotation applied.
Is there a greedy shortcut or do I really need to check all substrings?+
No shortcut. You need to check all possible substrings because rotating a small substring early in the string might produce a smaller result than rotating a longer one later. Brute force enumeration is the standard approach for this problem.