← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Jun 2026

Summary

Google SWE coding round with a frequency-based sorting problem. Pretty standard algorithmic stuff but the edge cases around tie-breaking tripped me up a bit.

Questions Asked (1)

Q1

Given an array of integers, return all distinct elements sorted by decreasing frequency. If two elements share the same frequency, sort them by increasing value.

Algorithms & Data Structures
Author's notes

My first instinct was a hash map for counts, then sort with a custom comparator.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then propose a solution using a hash map to count frequencies, followed by sorting the distinct elements with a custom comparator. Discuss the time and space complexity and consider potential optimizations or alternative approaches.

Pro tip: Mention that you can achieve O(n log n) time by sorting the distinct elements, but if the frequency range is small, you could use bucket sort to get O(n) time. This shows awareness of trade-offs and optimization.

1. Clarify requirements and edge cases

Ask about input size, range of integers, and whether the array can be empty or contain duplicates. Confirm that output should be distinct elements sorted by decreasing frequency and increasing value for ties.

2. Count frequencies

Use a hash map to count the frequency of each distinct element in O(n) time.

3. Sort distinct elements

Extract the distinct elements and sort them using a custom comparator: primarily by decreasing frequency, and for equal frequencies, by increasing value.

4. Analyze complexity and optimize

State that the sorting step takes O(d log d) where d is the number of distinct elements, leading to O(n + d log d) overall. Mention that bucket sort can achieve O(n) if frequencies are bounded.

5. Test with examples

Walk through a small example to verify correctness, such as [4,4,4,2,2,1,1,1,3] resulting in [4,1,2,3].

Key Points to Mention

  • Hash map for frequency counting
  • Custom comparator for sorting
  • Time complexity: O(n + d log d) where d is number of distinct elements
  • Space complexity: O(d) for the hash map and output
  • Edge cases: empty array, all elements same, all distinct
  • Alternative: bucket sort for O(n) if frequency range is small

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.