← Back
00:00

API Credit Manager

xAI interview questions · Hard · Coding round · 6 tests

Your job is to implement credit accounting for API usage on a cloud service. Every account is governed by its own policy, made of three values:

  • maxCredits - the most credits the account can ever hold
  • refillAmount - the number of credits added per refill
  • refillIntervalMs - the length of time between refills

Nothing refills in the background; refills happen lazily. Each time an account is accessed, work out how many complete refill intervals have passed since the last refill, add the corresponding credits, and clamp the result so it never goes above maxCredits.

Interface

Idle