Maximize Score Spending and Recovering Credits on Quota Tokens

Last updated: November 29, 2026

Quick Overview

You hold some API credits and a bag of quota tokens with values. Playing a token face up costs its value in credits and gains 1 score; playing it face down costs 1 score and returns its value in credits. Each token is used at most once; return the maximum score reachable.

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

0

0

0 solved


You hold some API credits and a bag of quota tokens with values. Playing a token face up costs its value in credits and gains 1 score; playing it face down costs 1 score and returns its value in credits. Each token is used at most once; return the maximum score reachable.

A two-pointer greedy in Google's style that tests whether you see buy-cheap, sell-expensive. The interviewer checks that you only trade score for credits when it enables more purchases, and that you track the best score rather than the final one.

What the Interviewer Expects
  • Sort tokens
  • Buy the cheapest token face up while affordable
  • Sell the most expensive token face down when stuck and score is positive
  • Track the maximum score seen
  • O(n log n) time
Key Topics to Cover
Greedy
Two pointers
Sorting
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
  • Selling face down gives back only half the value.
  • Each token can be played face up twice.
  • Return the sequence of plays.
  • Prove that never selling a cheap token is safe.
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 token values and starting credits. Face-up needs credits >= value; face-down needs score >= 1. Output: maximum score at any point....

High-Level Design

Score is gained one point at a time regardless of which token is bought, so buy the cheapest available token. Credits are recovered by spending one score, so sell the most expensive token to recover t...


Submit Your Answer
Markdown supported