Fewest Invoices Removed From Either End to Clear a Credit Balance

Last updated: November 29, 2026

Quick Overview

A billing queue holds positive invoice amounts. You may repeatedly remove the first or last invoice and subtract it from a credit x. Return the minimum number of removals that brings x to exactly zero, or -1 if impossible.

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

0

0

0 solved


A billing queue holds positive invoice amounts. You may repeatedly remove the first or last invoice and subtract it from a credit x. Return the minimum number of removals that brings x to exactly zero, or -1 if impossible.

A Google onsite problem framed around Google Cloud billing credits, where the key move is inverting the question: removing from both ends means keeping a contiguous middle. The interviewer checks that the candidate turns it into a longest window with a fixed sum.

What the Interviewer Expects
  • Reframes as the longest middle window summing to total minus x
  • Uses a positive-value sliding window
  • Handles the case where all invoices must be removed
  • Returns -1 when the target is negative or unreachable
  • States O(n) time and O(1) space
Key Topics to Cover
Sliding Window
Problem Reframing
Prefix and Suffix
Two Pointers
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
  • What if invoice amounts can be zero or negative?
  • Return which invoices to remove from each end.
  • What if removals from the left and right have different costs?
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 inv of positive integers, integer x > 0.
  • Each removal takes from the front or back.
  • Output: the minimum count of removals summing to exactly x, or -1....
High-Level Design

Whatever we remove is a prefix plus a suffix, so what remains is a contiguous middle block whose sum is total - x. Minimizing removals is maximizing the length of that block. So compute `need = tota...


Submit Your Answer
Markdown supported