PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Optiver

Make Two Apple Piles Equal With Doubling Moves

Last updated: Jul 2, 2026

Make Two Apple Piles Equal With Doubling Moves

Company: Optiver

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You have two piles of apples: the first pile contains `a` apples and the second pile contains `b` apples. In one move you choose one pile to be the **receiving** pile and move apples into it from the other pile. The number of apples moved must be exactly the number of apples the receiving pile currently contains, so the receiving pile doubles in size. A move is legal only if the giving pile holds at least that many apples. Determine whether there is a finite sequence of moves after which both piles contain the same number of apples. Return `true` if it is possible (including when the piles are already equal), and `false` otherwise. **Function signature** ``` canBalance(a, b) -> boolean ``` **Input** - `a` — the number of apples in the first pile - `b` — the number of apples in the second pile **Output** - `true` if the two piles can be made equal through some sequence of legal moves, `false` otherwise. **Constraints** - `1 <= a, b <= 10^9` - The answer must be produced efficiently for values up to the full constraint range. **Examples** 1. `a = 3, b = 5` → `true` Move 3 apples from the second pile onto the first: `(3, 5) → (6, 2)`. Then move 2 apples from the first pile onto the second: `(6, 2) → (4, 4)`. The piles are equal. 2. `a = 1, b = 2` → `false` The reachable states cycle: `(1, 2) → (2, 1) → (1, 2) → ...` — the piles never become equal. 3. `a = 2, b = 4` → `false` `(2, 4) → (4, 2) → (2, 4) → ...` — the states repeat and equality is never reached. 4. `a = 7, b = 7` → `true` The piles are already equal; zero moves are needed.

Related Interview Questions

  • Build and Validate a Binary Tree from Parent-Child Pairs - Optiver (medium)
  • Days Between Two Calendar Dates - Optiver (medium)
  • Thread-Safe Stock Inventory: Buy and Sell Without Overselling - Optiver (medium)
  • Find missing numbers in sequences - Optiver (hard)
  • Design a circular queue data structure - Optiver (medium)
|Home/Coding & Algorithms/Optiver

Make Two Apple Piles Equal With Doubling Moves

Optiver logo
Optiver
Mar 21, 2025, 12:00 AM
mediumData ScientistTechnical ScreenCoding & Algorithms
0
0

You have two piles of apples: the first pile contains a apples and the second pile contains b apples.

In one move you choose one pile to be the receiving pile and move apples into it from the other pile. The number of apples moved must be exactly the number of apples the receiving pile currently contains, so the receiving pile doubles in size. A move is legal only if the giving pile holds at least that many apples.

Determine whether there is a finite sequence of moves after which both piles contain the same number of apples. Return true if it is possible (including when the piles are already equal), and false otherwise.

Function signature

canBalance(a, b) -> boolean

Input

  • a — the number of apples in the first pile
  • b — the number of apples in the second pile

Output

  • true if the two piles can be made equal through some sequence of legal moves, false otherwise.

Constraints

  • 1 <= a, b <= 10^9
  • The answer must be produced efficiently for values up to the full constraint range.

Examples

  1. a = 3, b = 5 → true Move 3 apples from the second pile onto the first: (3, 5) → (6, 2) . Then move 2 apples from the first pile onto the second: (6, 2) → (4, 4) . The piles are equal.
  2. a = 1, b = 2 → false The reachable states cycle: (1, 2) → (2, 1) → (1, 2) → ... — the piles never become equal.
  3. a = 2, b = 4 → false (2, 4) → (4, 2) → (2, 4) → ... — the states repeat and equality is never reached.
  4. a = 7, b = 7 → true The piles are already equal; zero moves are needed.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Optiver•More Data Scientist•Optiver Data Scientist•Optiver Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.