← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Amazon SWE online assessment with a frequency-based array problem. Pretty standard algorithmic stuff but the one-pass constraint is what makes it interesting.

Questions Asked (1)

Q1

Given an array of integers, find the top k most frequent elements using only a single linear scan. Output them sorted by frequency descending, and by value ascending when frequencies tie.

Algorithms & Data Structures
Author's notes

The single-pass constraint is what tripped me up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that a single linear scan means O(n) time, so we need a hash map for frequency counting and a bucket sort (array of lists indexed by frequency) to avoid O(n log n) sorting. Then extract the top k by iterating buckets from highest frequency to lowest, and for ties, sort the elements within each bucket by value ascending.

Pro tip: Mention that bucket sort works because frequencies are bounded by n, and that tie-breaking by value can be handled by sorting each bucket or by using a min-heap of size k with a custom comparator. Also note that if k is small, a heap might be more space-efficient, but bucket sort is simpler for O(n) time.

1. Clarify requirements and constraints

Confirm that 'single linear scan' means O(n) time and that we can use O(n) extra space. Ask about input size, whether k can be larger than the number of distinct elements, and if the output should be a list of elements or frequencies.

2. Count frequencies in one pass

Use a hash map to count the frequency of each element in a single pass through the array. This is the only pass over the input array.

3. Bucket elements by frequency

Create an array of lists (buckets) where the index represents frequency (from 1 to n). Place each distinct element into the bucket corresponding to its frequency. This avoids sorting by frequency.

4. Extract top k with tie-breaking

Iterate buckets from highest frequency to lowest. Within each bucket, sort elements by value ascending (or use a min-heap of size k with comparator: higher frequency first, then lower value). Collect until k elements are found.

5. Analyze complexity and edge cases

State time complexity: O(n) for counting + O(n) for bucket creation + O(k log k) for sorting within buckets (or O(n log k) with heap). Space: O(n). Discuss edge cases: k=0, k > distinct elements, all elements same frequency.

Key Points to Mention

  • Hash map for frequency counting in one pass over the input array.
  • Bucket sort (array of lists indexed by frequency) to achieve O(n) time without comparison sorting.
  • Tie-breaking by value ascending: sort each bucket's elements or use a custom comparator in a heap.
  • Time complexity: O(n + k log k) with bucket sort, or O(n log k) with a min-heap of size k.
  • Space complexity: O(n) for the hash map and buckets.
  • Edge cases: k=0, k larger than distinct elements, negative numbers, and all elements having the same frequency.

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