FastPrepGroup Anagrams
Problems/Group Anagrams

Group Anagrams

Expedia logoExpedia● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An Expedia SDE intern review lists this as the second of its two coding questions, from the interview rounds after the online assessment. The candidate names the task, grouping anagrams, and calls it a basic practice question.

The wording mentions only a string and gives no fuller input format, order rule or limits. We read it as the usual version that takes a list of words. Here, the groups come in the order their first word appears. Words keep their input order inside a group. Repeated words and empty strings are kept. The limits are also our choice: up to 1000 words of up to 20 lowercase letters.

Our version is about 80% the same as the reported question.

You are given an array of strings words. Two words are anagrams when one can be made by reordering the letters of the other, so they use the same letters the same number of times. An empty string is an anagram of another empty string.

Group the words so that each group holds exactly the words that are anagrams of each other. Return the groups in this order:

  • The groups appear in the order in which their first word appears in words.
  • Inside a group, the words keep their order from words.
  • Repeated words are kept. Every word of words appears exactly once in the output.

Function

groupAnagrams(words: String[]) → List<List<String>>

Examples

Example 1

words = ["rail","trip","liar","seat","lair","east","teas"]return = [["rail","liar","lair"],["trip"],["seat","east","teas"]]

rail, liar and lair all use the letters a, i, l, r once each, so they form one group. seat, east and teas form another. trip has no partner. The groups appear in the order their first words appear: rail (index 0), trip (index 1), seat (index 3).

Example 2

words = ["no","on","","on",""]return = [["no","on","on"],["",""]]

Both copies of on stay in the output, next to no. The two empty strings are anagrams of each other, so they form the second group.

Example 3

words = ["pool","loop","polo","lop","plop"]return = [["pool","loop","polo"],["lop"],["plop"]]

pool, loop and polo each have one p, two os and one l. lop has only one o, and plop has two ps and one o. Using the same letters is not enough: the counts must match too, so these two are each on their own.

Constraints

  • 1 <= words.length <= 1000
  • 0 <= words[i].length <= 20
  • words[i] contains only lowercase English letters.

More Expedia Onsite Interview questions

  • Scramble StringONSITE INTERVIEW · Seen Sep 2026
  • Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
  • Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
  • Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
  • Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
  • Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
  • Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
  • Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
  • Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
  • Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024

Similar questions at other companies

  • Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
  • Group AnagramsMicrosoft · ONSITE INTERVIEW
  • Find Duplicate Element PairsGoogle · ONSITE INTERVIEW
  • Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEW
  • Group Duplicate Files by ContentPostman · ONSITE INTERVIEW
  • Group AnagramsGoldman Sachs · ONSITE INTERVIEW
  • Group AnagramsMotive · PHONE SCREEN · ONSITE INTERVIEW
  • Group AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
  • Group AnagramsAkuna Capital · ONSITE INTERVIEW
  • Group Anagram StringsLondon Stock Exchange · ONSITE INTERVIEW
See Expedia hiring insights
Saved
class Solution {
    public List<List<String>> groupAnagrams(String[] words) {
        // Write your code here.
    }
}
Checking account…