Arranging Coins

EasyBinary Searchbinary search on answer

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You have n coins and want to build a staircase: row 1 gets 1 coin, row 2 gets 2 coins, row k gets k coins. The last row you start may end up incomplete. Return the number of **complete** rows you can build — the largest k with 1 + 2 + ... + k <= n. Aim for O(log n) time via binary search (or O(1) with care for integer precision).

Examples

Input: n = 5
Output: 2
Rows use 1 and 2 coins (3 total); the third row would need 3 but only 2 remain.
Input: n = 8
Output: 3
1 + 2 + 3 = 6 coins fill three rows; the fourth row gets only 2 of 4.

Constraints

  • 1 <= n <= 2^31 - 1
  • Rows are filled in order; row `k` needs exactly `k` coins.
  • Return a non-negative integer.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search on answer problems