Count Num Ways
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 that asks you to count how many substrings of length k, when reversed, make the entire string lexicographically smaller. This is a substring enumeration problem wrapped in a hashing context. The trick is understanding what "lexicographically smaller" actually means here: you reverse a contiguous k-length substring and compare the result to the original. Only reversals that produce a smaller string count. StealthCoder will spot the pattern and the edge cases if you blank on the logic.
The problem
Amazon is working on a new hashing approach that takes in the original string and a seed number. Engineers decided that the seed can be generated from the same input string by counting the number of times a reverse of a substring of length k makes the new string lexicographically smaller. You are deployed with the task of developing a service that takes in a string s and an integer k, and returns the number of ways to reverse any substring of length k such that the resulting string is lexicographically smaller than the original string. Note: 1. A substring is a contiguous sequence of characters within a string. For example, the string "zon" is a substring of "amazon", "zone", etc.but is not a substring of "zoin", "zozo", etc. 2. A string a is lexicographically smaller than string b if a[i] at the first index where a and b differ. For example, "amazon" is lexicographically smaller than "amozan". Function Description: 1. Complete the function countNumWays in the editor. 2. countNumWays has the following parameters: a. string s:the original string b. int k:the algorithm parameter Returns: int: the number of possible ways to perform the operation ensuring the given constraint
Reported by candidates. Source: FastPrep
Pattern and pitfall
The core pattern is substring reversal with lexicographic comparison. You iterate through all possible starting positions in the string (there are len(s) - k + 1 of them), reverse the substring at that position, and check if the new string is lexicographically smaller than the original. The comparison happens at the first differing character. Many candidates miss that you're comparing the entire resulting string, not just the reversed segment. The pitfall: checking only the reversed portion instead of the whole string, or assuming a reversal always makes it smaller (it doesn't). A brute-force approach works fine for reasonable string lengths. StealthCoder's role is your insurance policy: if you freeze on the comparison logic during the live OA, you have a reference implementation in real time.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Num Ways 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.
Count Num Ways FAQ
Does reversing a substring always make the string lexicographically smaller?+
No. You must actually compare the entire resulting string to the original. For example, reversing "ba" in "bac" gives "abc", which is smaller. But reversing "ab" in "abc" gives "bac", which is larger. Only count reversals where the full result is smaller.
What's the time complexity for a brute-force solution?+
O(n^2) for iterating substrings and O(n) for each string comparison, so O(n^3) overall. For strings under a few thousand characters, this is acceptable in most OA time windows. Optimize only if you hit a timeout.
How do I check if one string is lexicographically smaller in code?+
Use built-in string comparison. In Python, just use the less-than operator on strings. In Java or C++, use compareTo or the appropriate comparison function. The language handles character-by-character comparison for you.
What if k equals the length of the string?+
You reverse the entire string once and check if it's smaller than the original. That's one possible operation. For example, reversing "ba" gives "ab", which is lexicographically smaller, so the count is 1.
Is this problem asking for distinct reversals or all positions?+
All positions. You count every starting index where reversing a k-length substring produces a smaller string. Different positions are counted separately, even if they result in the same string or overlap.