Distribute Candies to People

EasyMathsimulation

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 candies candies to hand out to num_people people standing in a row, numbered 0 to num_people - 1. Walk along the row repeatedly. The first gift is 1 candy (to person 0), the second gift is 2 candies (to person 1), and so on: every gift is one candy larger than the previous one, and after the last person you wrap back to person 0 and keep going. When the next gift would be larger than the candies left, the current person gets all the remaining candies and the process stops. Return an array of length num_people whose i-th entry is the total number of candies person i received.

Examples

Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Gifts of 1, 2, 3 use 6 candies; person 3 gets the last 1.
Input: candies = 10, num_people = 3
Output: [5,2,3]
Gifts 1, 2, 3 go around once; person 0 would need 4 and exactly 4 remain.

Constraints

  • 1 <= candies <= 10^9
  • 1 <= num_people <= 1000

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 simulation problems