Skip to main content

49. Group Anagrams

mediumAsked at Juniper Networks

Group strings that are anagrams of each other. Juniper uses this to test canonical-key hashing — the same pattern appears when normalizing and grouping interface configuration commands that differ only in token order before feeding them to a configuration management pipeline.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Juniper Networks loops.

  • Glassdoor (2025-Q3)Listed in Juniper SWE onsite reports as a hash-map design question.
  • Blind (2025-09)Juniper candidates mention Group Anagrams as a medium-level grouping/hashing problem in technical screens.

Problem

Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Constraints

  • 1 <= strs.length <= 10^4
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.

Examples

Example 1

Input
strs = ["eat","tea","tan","ate","nat","bat"]
Output
[["bat"],["nat","tan"],["ate","eat","tea"]]

Explanation: Three anagram groups.

Example 2

Input
strs = [""]
Output
[[""]]

Explanation: Single empty string forms its own group.

Example 3

Input
strs = ["a"]
Output
[["a"]]

Explanation: Single character.

Approaches

1. Sorted string as key

Sort each string alphabetically to produce a canonical key. All anagrams produce the same key. Group strings by key in a Map.

Time
O(n * k log k) where k = max string length
Space
O(n * k)
function groupAnagrams(strs) {
  const map = new Map();
  for (const s of strs) {
    const key = s.split('').sort().join('');
    if (!map.has(key)) map.set(key, []);
    map.get(key).push(s);
  }
  return [...map.values()];
}

Tradeoff: Clean and easy to implement. O(n * k log k) due to the sort. Good first answer; then offer the frequency-count optimization.

2. Character frequency array as key

Instead of sorting, build a 26-element frequency count for each string. Stringify the count array as the key. Avoids the k log k sort.

Time
O(n * k)
Space
O(n * k)
function groupAnagrams(strs) {
  const map = new Map();
  for (const s of strs) {
    const count = new Array(26).fill(0);
    for (const ch of s) count[ch.charCodeAt(0) - 97]++;
    const key = count.join(',');
    if (!map.has(key)) map.set(key, []);
    map.get(key).push(s);
  }
  return [...map.values()];
}

Tradeoff: O(n * k) — replaces the sort with a linear frequency sweep. The key is a 26-element comma-separated string. Preferred when k is large.

Juniper Networks-specific tips

Lead with the sorted-key approach because it is simplest to explain, then proactively offer the frequency-count optimization and state the complexity difference. Juniper interviewers appreciate engineers who know when to optimize and why — the k log k vs k tradeoff is exactly that kind of discussion. Also discuss whether the key needs a delimiter ('1,0,2' vs '102') to avoid ambiguity between different frequency distributions.

Common mistakes

  • Forgetting the delimiter when joining the frequency array — '10,2' and '1,0,2' are different distributions but '102' is ambiguous.
  • Using a plain object as the map instead of Map — plain objects coerce keys to strings, which can cause subtle bugs.
  • Sorting in place and comparing with === — you need to reconstruct the original strings, not their sorted versions.
  • Returning map.values() (an iterator) instead of spreading it into an array — may cause serialization issues.

Follow-up questions

An interviewer at Juniper Networks may pivot to one of these next:

  • Valid Anagram (LC 242) — check if two strings are anagrams of each other.
  • Find All Anagrams in a String (LC 438) — sliding window over a string to find anagram positions.
  • How would you group routing commands that differ only in token order?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does sorting produce the same key for anagrams?

Anagrams contain the same characters with the same frequencies. Sorting rearranges them into the same canonical order, producing an identical string for all anagrams of a given set.

Why use a comma delimiter in the frequency key?

Without a delimiter, counts like [1,12] and [11,2] would both produce '112'. The comma makes each count unambiguous.

Is the output order guaranteed?

No — the problem says 'any order'. The order of groups and the order within each group can vary.

Practice these live with InterviewChamp.AI

Drill Group Anagrams and other Juniper Networks interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →