InterviewDryRun
Coding interview · Asked at junior and mid level

Group Anagrams

How the interviewer asks it

“Given a list of words, group together the words that are anagrams of each other.”
Sample test input
eat tea tan ate nat bat
Expected output
ate eat tea
bat
nat tan

In the practice room your code reads the input from standard input, and Run checks it against tests like this one. Try it now in your browser, in Python or JavaScript.

Typed drills are free and unlimited: the AI scores your answer against the outline below.

What the interviewer is looking for

  • You find a canonical key that every anagram shares.
  • You compare sorted-string keys (O(k log k) per word) with letter-count keys (O(k)).
  • You ask about the alphabet before choosing a 26-slot count.

How to structure your answer

  1. 01

    Clarify

    Lower-case English only? Does output order matter? Are duplicates possible?

  2. 02

    Key

    Sort each word's letters, or build a 26-number count, and use it as the map key.

  3. 03

    Group

    Map from key to a list of words; return the lists.

  4. 04

    Complexity

    O(n × k log k) with sorted keys, O(n × k) with counts, for n words of length k.

Best answers from candidates

No candidate answers yet. Type yours, and if it scores 6 or more you can show it here without your name.

Answers are shared by candidates, without names, after a privacy check. Add yours

Common mistakes

  • Using a sum or product of letter codes as the key, which collides.
  • Comparing every pair of words, which is O(n²).
  • Building a count key without a separator, so counts like 1,11 and 11,1 collide.

Follow-up questions to expect

  • The words do not fit in memory on one machine.
  • Words may contain any Unicode letter.

Now answer it out loud

Max will ask you this exact question, listen to your answer, push on the weak spots with follow-ups like the ones above, and score you when you finish.

Practice with Max

Free: a 5-minute voice round and unlimited typed drills, with scored feedback. No card, and your camera is never recorded.

More coding interview questions

Preparing for this round? Read how to prepare for a coding interview: a four-week DSA plan.