Find Two Ad Bids in a Sorted List That Hit a Budget

Last updated: November 29, 2026

Quick Overview

Given ad bids sorted in ascending order and a campaign budget, return the indices of two different bids whose sum equals the budget exactly, or an empty list if no such pair exists.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given ad bids sorted in ascending order and a campaign budget, return the indices of two different bids whose sum equals the budget exactly, or an empty list if no such pair exists.

A Google Ads flavored warm-up. Because the input is sorted, the interviewer expects the candidate to notice that a hash map is unnecessary and that two pointers give O(1) space, then to prove why moving the pointers never skips a valid pair.

What the Interviewer Expects
  • Notices the input is sorted and uses converging pointers
  • Explains why discarding an index is safe
  • Returns indices of two distinct positions
  • Handles no-solution and duplicate values
  • States O(n) time and O(1) space
Key Topics to Cover
Two Pointers
Sorted Arrays
Pair Sum
Correctness Proof
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
  • How would you return every distinct pair of values that hits the budget?
  • What if the list is unsorted and you can only use O(1) extra space?
  • How would you count pairs whose sum is at most the budget?
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: bids sorted ascending, integer budget.
  • Output: [i, j] with i < j and bids[i] + bids[j] == budget, or [].
  • Two different positions must be used, though they may hold equal value...
High-Level Design

Start with lo = 0 and hi = n - 1. If the sum is too small, the smallest bid bids[lo] cannot pair with anything to reach the budget, because its best partner is already the largest remaining bid,...


Submit Your Answer
Markdown supported