Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Heaviest Coin Bag Algorithm

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Heaviest Coin Bag Algorithm. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

During an NVIDIA H100 validation run, a calibration fixture contains 10 labeled bags, each holding 10 coins. Nine bags contain coins weighing normal_weight grams, while every coin in exactly one bag weighs heavy_weight grams. The heavier bag must be identified with one weighing.

Before weighing, remove 1 coin from bag 1, 2 coins from bag 2, and so on through 10 coins from bag 10. Weigh all 55 selected coins together. Implement a function that returns the 1-based label of the bag containing the heavier coins.

Formal Specification

Implement find_heavy_bag(measured_weight, normal_weight, heavy_weight).

  • measured_weight, normal_weight, and heavy_weight are positive integers representing grams.
  • The function returns an integer from 1 through 10.
  • Assume heavy_weight > normal_weight and exactly one bag is heavy.

Constraints

  • There are exactly 10 bags.
  • Each bag contains exactly 10 coins.
  • 1 <= normal_weight < heavy_weight <= 1000.
  • The measured weight exactly matches one valid heavy-bag configuration.
  • The returned bag label must be an integer from 1 through 10.

Function Signature

def find_heavy_bag(measured_weight, normal_weight, heavy_weight):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output