Reported July 2026
MathWorkshash table

Group Shifted Strings

Reported by candidates from MathWorks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live MathWorks OA. Under 2s to a working solution.
Founder's read

MathWorks reported this one in July 2026, and it looks like a string problem but it's really a hashing problem in disguise. Group Shifted Strings asks you to bucket words that are the same shape under a cyclic alphabet shift. If you've got an OA invite for this, the whole job is building one canonical key per string, then letting a hash map do the grouping. Order rules are the only catch. If you blank on the key, StealthCoder runs invisibly on your screen during the live OA as a safety net, but the idea is small enough to own tonight.

The problem

You are given an array of non-empty lowercase English strings. Two strings belong to the same shifting group when they have the same length and one can be transformed into the other by shifting every character forward by the same number of alphabet positions, wrapping from z to a.
Return all shifting groups. Scan strings from left to right: groups must appear in the order of their first member, and strings within each group must retain their input order. Preserve every occurrence, including duplicate strings.

Function
groupStrings(strings: String[]) → String[][]

Examples
Example 1
strings = ["abc","bcd","acef","xyz","az","ba","a","z"]
return = [["abc","bcd","xyz"],["acef"],["az","ba"],["a","z"]]
The strings abc, bcd, and xyz share the same cyclic differences between consecutive characters. The pair az and ba also matches across the alphabet boundary, while all one-character strings form one group. The group and member order follows the input.
Example 2
strings = ["a","b","a","aa","bb","za"]
return = [["a","b","a"],["aa","bb"],["za"]]
Every one-character string belongs to the same group, and the second occurrence of a is preserved. The strings aa and bb share a zero-difference pattern, while za has a different cyclic pattern.

Constraints
1 <= strings.length <= 10^4
1 <= strings[i].length <= 50
The total number of characters across strings is at most 10^5.
Every string contains only lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: two strings are in the same group if their consecutive character differences match modulo 26. So for each string, compute (s[i] - s[i-1] + 26) % 26 for every adjacent pair, join those numbers with a separator, and use that as the map key. Length is baked into the key automatically, and every one-character string gets an empty key, so they all group together. The pitfall is skipping the +26 before the modulo, which breaks wraparound cases like az and ba. Another pitfall is joining digits without a delimiter, which makes 1,12 collide with 11,2. Use a map that remembers insertion order, or store a list of keys as you first see them, so groups come out by first member. Duplicates just get appended. Total work is linear in total characters. If the key logic slips under pressure, StealthCoder is the hedge for the live OA.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Group Shifted Strings 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as group shifted strings. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass MathWorks's OA.

MathWorks reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Group Shifted Strings FAQ

What's the trick in Group Shifted Strings?+

Normalize each string into a key made of cyclic differences between adjacent letters, computed as (diff + 26) % 26. Strings that shift together produce identical keys. Then group by key in a hash map. Everything else is just keeping output order correct.

How do I keep the group order the MathWorks problem wants?+

Groups must appear in order of their first member, and members keep input order. Scan left to right, and when you see a new key, record it in a key-order list or use an insertion-ordered map. Append each string to its key's list. Output lists in key-order.

How do I handle one-character strings and duplicates?+

A single character has no adjacent pairs, so its key is an empty string, and all one-letter strings land in one group. Duplicates aren't deduplicated. Each occurrence gets appended to its group, as Example 2 shows with the repeated a.

What's the most common bug on this problem?+

Forgetting wraparound. Computing s[i] - s[i-1] without adding 26 gives negative values, so az and ba won't match. Also watch delimiters in the key. Joining numbers with no separator can make different difference sequences collide.

How do I prepare for this in 48 hours?+

Write the solution once from scratch, then test it on both examples by hand, especially az/ba and the one-character cases. Know the complexity: linear in total characters, under 10^5 here. Then do two or three other hash-map grouping problems so the pattern feels automatic.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with MathWorks.

OA at MathWorks?
Invisible during screen share
Get it