Find the heavier of 8 balls in two balance-scale weighings and log each weighing

Quick Overview

Simulate a two-weighing strategy that finds the one heavier ball among eight using a balance scale, and return a log of each weighing and its result. It tests reasoning about three-way outcomes, the lower bound on the number of weighings, and exact string output.

Find the heavier of 8 balls in two balance-scale weighings and log each weighing

Company: Thoughtworks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given `8` balls numbered `0` through `7`. Exactly one of them is slightly heavier than the rest, which all weigh the same. You have a balance scale that compares two groups of balls and shows one of three results: the left side is heavier, the right side is heavier, or the two sides balance. Given the index of the heavy ball, `heavyBall`, return a list of strings describing the sequence of weighings that identifies it using the minimum number of weighings. You must identify the heavy ball in exactly 2 weighings. ### Function Signature ```python def find_heavy_ball(heavy_ball: int) -> list[str]: ``` ### Rules - Return exactly two strings, one per weighing, in the order the weighings happen. - The first weighing is always balls `[0,1,2]` on the left against `[3,4,5]` on the right. - The second weighing puts the two lowest-numbered balls of the remaining suspects on the scale, the lower one on the left: `[0] vs [1]`, `[3] vs [4]` or `[6] vs [7]`. - A result is written as exactly one of `Left side is heavier`, `Right side is heavier` or `They balance`. - The first string has the form `Weigh [0,1,2] vs [3,4,5]: <result>, heavy ball is among <group>`, where `<group>` is `[0,1,2]`, `[3,4,5]` or `[6,7]`. - The second string has the form `Weigh [a] vs [b]: <result>, heavy ball is <k>`, where `k` is the heavy ball's number. - Groups are written with no spaces inside the brackets. ### Constraints - `0 <= heavyBall <= 7` - There are exactly `8` balls. - Exactly one ball is heavier than the others. - The heavy ball must be identified in exactly `2` weighings. ### Examples ```text Input: heavy_ball = 0 Output: ["Weigh [0,1,2] vs [3,4,5]: Left side is heavier, heavy ball is among [0,1,2]", "Weigh [0] vs [1]: Left side is heavier, heavy ball is 0"] ``` Ball 0 is on the left pan in the first weighing, so the left side goes down and the suspects are 0, 1 and 2. In the second weighing ball 0 is on the left again, so the left side is heavier and ball 0 is the heavy one. ```text Input: heavy_ball = 7 Output: ["Weigh [0,1,2] vs [3,4,5]: They balance, heavy ball is among [6,7]", "Weigh [6] vs [7]: Right side is heavier, heavy ball is 7"] ``` Ball 7 is off the scale in the first weighing, so the pans balance and the suspects are 6 and 7. Weighing 6 against 7 tips to the right, which identifies ball 7. ```text Input: heavy_ball = 5 Output: ["Weigh [0,1,2] vs [3,4,5]: Right side is heavier, heavy ball is among [3,4,5]", "Weigh [3] vs [4]: They balance, heavy ball is 5"] ``` Ball 5 is on the right pan first, so the suspects are 3, 4 and 5. It is left off the scale in the second weighing, so 3 and 4 balance and the heavy ball must be 5.

Overview: Simulate a two-weighing strategy that finds the one heavier ball among eight using a balance scale, and return a log of each weighing and its result. It tests reasoning about three-way outcomes, the lower bound on the number of weighings, and exact string output.

Thoughtworks logo
Thoughtworks
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given 8 balls numbered 0 through 7. Exactly one of them is slightly heavier than the rest, which all weigh the same.

You have a balance scale that compares two groups of balls and shows one of three results: the left side is heavier, the right side is heavier, or the two sides balance. Given the index of the heavy ball, heavyBall, return a list of strings describing the sequence of weighings that identifies it using the minimum number of weighings. You must identify the heavy ball in exactly 2 weighings.

Function Signature

def find_heavy_ball(heavy_ball: int) -> list[str]:

Rules

  • Return exactly two strings, one per weighing, in the order the weighings happen.
  • The first weighing is always balls [0,1,2] on the left against [3,4,5] on the right.
  • The second weighing puts the two lowest-numbered balls of the remaining suspects on the scale, the lower one on the left: [0] vs [1] , [3] vs [4] or [6] vs [7] .
  • A result is written as exactly one of Left side is heavier , Right side is heavier or They balance .
  • The first string has the form Weigh [0,1,2] vs [3,4,5]: <result>, heavy ball is among <group> , where <group> is [0,1,2] , [3,4,5] or [6,7] .
  • The second string has the form Weigh [a] vs [b]: <result>, heavy ball is <k> , where k is the heavy ball's number.
  • Groups are written with no spaces inside the brackets.

Constraints

  • 0 <= heavyBall <= 7
  • There are exactly 8 balls.
  • Exactly one ball is heavier than the others.
  • The heavy ball must be identified in exactly 2 weighings.

Examples

Input: heavy_ball = 0
Output: ["Weigh [0,1,2] vs [3,4,5]: Left side is heavier, heavy ball is among [0,1,2]", "Weigh [0] vs [1]: Left side is heavier, heavy ball is 0"]

Ball 0 is on the left pan in the first weighing, so the left side goes down and the suspects are 0, 1 and 2. In the second weighing ball 0 is on the left again, so the left side is heavier and ball 0 is the heavy one.

Input: heavy_ball = 7
Output: ["Weigh [0,1,2] vs [3,4,5]: They balance, heavy ball is among [6,7]", "Weigh [6] vs [7]: Right side is heavier, heavy ball is 7"]

Ball 7 is off the scale in the first weighing, so the pans balance and the suspects are 6 and 7. Weighing 6 against 7 tips to the right, which identifies ball 7.

Input: heavy_ball = 5
Output: ["Weigh [0,1,2] vs [3,4,5]: Right side is heavier, heavy ball is among [3,4,5]", "Weigh [3] vs [4]: They balance, heavy ball is 5"]

Ball 5 is on the right pan first, so the suspects are 3, 4 and 5. It is left off the scale in the second weighing, so 3 and 4 balance and the heavy ball must be 5.