Count Trip Pairs Whose Fares Exactly Match a Promo Credit

Last updated: November 29, 2026

Quick Overview

Given an array of trip fares in cents and a promo credit value, count the number of index pairs i < j whose fares sum exactly to the credit, so finance can estimate how many riders could fully spend a two-trip promotion.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Given an array of trip fares in cents and a promo credit value, count the number of index pairs i < j whose fares sum exactly to the credit, so finance can estimate how many riders could fully spend a two-trip promotion.

A warm-up in Uber technical screens that checks whether you can move from the O(n^2) brute force to a one-pass hash map count without over-engineering. Interviewers listen for whether you count pairs (including duplicates) rather than just detecting existence.

What the Interviewer Expects
  • State the brute force and its cost first
  • Use a frequency map of fares seen so far
  • Count all pairs, including repeated equal fares
  • Keep the solution to one pass
  • Mention integer cents to avoid float equality issues
Key Topics to Cover
hash map
two sum counting
frequency counting
integer money
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
  • Return the pairs themselves instead of the count; what changes in output size?
  • What if the array is already sorted and memory is tight?
  • How would you count triples summing to the credit?
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 fares of non-negative integers (cents), integer credit.
  • Output: number of pairs (i, j) with i < j and fares[i] + fares[j] == credit.
  • Equal fares at different indices are di...
High-Level Design

For each fare f, the partners that complete it are exactly the earlier fares equal to credit - f. If we keep a frequency map of fares seen so far, each step adds seen[credit - f] to the answer a...


Submit Your Answer
Markdown supported