Find Products Making Up More Than a Third of All Returns

Last updated: November 29, 2026

Quick Overview

Given a list of product IDs from return requests, return every product ID that appears more than n/3 times, using O(1) extra space.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a list of product IDs from return requests, return every product ID that appears more than n/3 times, using O(1) extra space.

An Amazon onsite coding question framed around spotting defective products from return logs. The interviewer expects a hash map answer first, then pushes for constant space, looking for the generalized Boyer-Moore voting algorithm and a correct verification pass.

What the Interviewer Expects
  • Start with a counting hash map and state its O(n) space
  • Argue that at most two IDs can exceed n/3
  • Implement two-candidate Boyer-Moore voting
  • Run a second pass to verify candidates
  • Handle the case where both candidates collide or are absent
Key Topics to Cover
Boyer-Moore Voting
Counting
Heavy Hitters
Arrays
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Generalize to every element appearing more than n/k times.
  • How would you find heavy hitters approximately in a stream that does not fit in memory?
  • Can you merge candidate summaries computed on separate shards?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of product IDs, length n, may be empty. Output: all IDs with count strictly greater than n // 3 (that is, more than n/3), any order. Target O(n) time and O(1) extra space....

High-Level Design

At most two values can each exceed a third of the list, since three of them would total more than n. Keep two candidates with counters:

  • If the item matches a candidate, increment that counter.
  • El...

Submit Your Answer
Markdown supported