Group Anagrams
Updated
Problem statement
You are given an array of lowercase strings strs. Group together exactly the words that are anagrams of one another: two words are anagrams when they contain the same letters with the same multiplicities.
Keep every input occurrence, including duplicate words and empty strings, with its original spelling. Each occurrence must appear in exactly one group.
For this practice version, sort the words inside each group in ascending lexicographic order. Then sort the groups by their first word in ascending lexicographic order. Every group is nonempty. Use ordinary lowercase ASCII order, where an empty string sorts before any nonempty string.
Return a new outer array and new group arrays. Do not change the input array or its word spellings. Use local state for every call; string values do not need separate copies.
C++ discussion follow-up
Consider the following fragment with a mutable vector<string> strs, i=0, a string-to-vector map named map, and a result vector named res.
for(string str: strs){
sort(str.begin(), str.end());
map[str].push_back(strs[i++]);
}
for(auto & m : map){
res.push_back(m.second);
}Explain why the first loop copies each string here. What happens if it uses a mutable reference and sorts that input element before appending strs[i++]? Explain what copying the reference in the second loop avoids, and whether iterating those map entries by value would still be functionally correct.
This fragment supplies discussion context. A complete solution must also perform the output-order normalization required above.
Function
groupAnagrams(strs: String[]) → List<List<String>>Examples
Example 1
strs = ["eat","tea","tan","ate","nat","bat"]return = [["ate","eat","tea"],["bat"],["nat","tan"]]The groups are the anagrams of ate, bat and nat. Sorting within them gives [ate,eat,tea], [bat] and [nat,tan]. Their first words are already in increasing order.
Example 2
strs = ["","b",""]return = [["",""],["b"]]The two empty strings form one group and both occurrences are kept. Its first word is empty, so that group comes before [b].
Example 3
strs = ["abc","bca","cab","ab","ba","a"]return = [["a"],["ab","ba"],["abc","bca","cab"]]The groups have letter multisets a, ab and abc. Their sorted first words are a, ab and abc, in that order.
Constraints
1 <= strs.length <= 25.- Each input string has length between
0and12, inclusive. - Every character in every input string is a lowercase English letter from a through z.
- Empty strings and duplicate words are allowed.
More Akuna Capital Onsite Interview questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Count Set Bits in an Integer ArrayONSITE INTERVIEW · Seen Jan 2026
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- First and Last Position in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2022
- Maximum Observation in a Time WindowPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2021
- Stream Median, Mean and MaximumPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2021
- Maximum Stock Profit with a Transaction LimitONSITE INTERVIEW · Seen Aug 2021
- Rotate a Square MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2020
- Trace of a Square MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen May 2019
Similar questions at other companies
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Find Duplicate Element PairsGoogle · ONSITE INTERVIEWGoogle interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Group Duplicate Files by ContentPostman · ONSITE INTERVIEWPostman interview questions
- Group AnagramsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Group AnagramsMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
- Group AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Group AnagramsExpedia · ONSITE INTERVIEWExpedia interview questions
- Group Anagram StringsLondon Stock Exchange · ONSITE INTERVIEWLondon Stock Exchange interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)