Round Nightly Price Breakdown So the Total Still Matches

Last updated: November 29, 2026

Quick Overview

A stay's price breakdown has fractional amounts (nightly rates after discounts, fees, taxes) that must be shown as whole dollars, and the displayed lines must add up exactly to the rounded total charged. Round each line to its floor or ceiling so the sum equals the target and the total absolute rounding error is minimized, or report that it is impossible.

Airbnb
Coding & Algorithms
Software Engineer
Airbnb
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A stay's price breakdown has fractional amounts (nightly rates after discounts, fees, taxes) that must be shown as whole dollars, and the displayed lines must add up exactly to the rounded total charged. Round each line to its floor or ceiling so the sum equals the target and the total absolute rounding error is minimized, or report that it is impossible.

Guests on the checkout page notice instantly when line items do not add up to the total, so Airbnb needs a rounding rule that is both consistent and minimally distorting. In an onsite coding round the interviewer wants to see you prove why a greedy choice is optimal, not just guess it.

What the Interviewer Expects
  • Recognize that each value only has two choices, floor or ceiling
  • Compute how many values must be rounded up from the floor sum and target
  • Prove that bumping the largest fractional parts minimizes error
  • Detect infeasible targets and integer-valued lines that cannot move
  • Avoid floating point traps by using a tolerance or integer cents
Key Topics to Cover
greedy
exchange argument
sorting
floating point precision
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 do this exactly with prices stored as integer cents?
  • What if some lines (like taxes) must never be rounded down?
  • How would you break ties so the same booking always renders the same breakdown?
  • What changes if the goal is minimizing the maximum single-line error?
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 non-negative floats and integer target. Output: integers r_i with r_i in {floor(p_i), ceil(p_i)}, sum r_i = target, minimizing sum |r_i - p_i|. Return None if no assignment exists. An i...

High-Level Design

Start with everything floored. The floor sum F is at most the target, and each bump adds exactly 1, so we need exactly need = target - F bumps. Bumping item i changes its error from f_i to 1 - f_i, ...


Submit Your Answer
Markdown supported