Check Whether Two Listed Prices Sum to a Gift Card Value

Last updated: November 29, 2026

Quick Overview

Design a structure where `add(price)` records a new integer price from a stream and `find(value)` reports whether any two recorded prices (distinct entries, possibly equal amounts) add up to the value.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Design a structure where `add(price)` records a new integer price from a stream and `find(value)` reports whether any two recorded prices (distinct entries, possibly equal amounts) add up to the value.

An online assessment style question in Amazon's style that asks you to choose which operation to make fast. It is framed around checking whether a gift card can be spent exactly on two items, and tests whether you reason about the read and write ratio before picking a design.

What the Interviewer Expects
  • Uses a count map so duplicate prices are handled
  • Handles value equal to twice a price only when it appears twice
  • Explains the trade-off of O(1) add with O(n) find versus the reverse
  • Picks a design based on stated call frequencies
  • Writes correct, tidy code
Key Topics to Cover
design a data structure
hash maps
trade-offs
two sum
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
  • If find is called 100 times more often than add, what would you change?
  • How would you support remove(price)?
  • How would you extend to three prices summing to the value?
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

Integer prices, possibly repeated. find(v) is True if two different entries sum to v. The interviewer will usually state call frequencies; ask, because it drives the design....

High-Level Design

Write-optimized: store a Counter. add is O(1)O(1); find scans distinct prices and checks the complement, treating p + p specially (needs count at least 2).

Read-optimized: maintain the ...


Submit Your Answer
Markdown supported