Woodstock Blog

a tech blog for general algorithmic interview questions

[Question] Breaking Chocolate Bars

Game #1

link

Two players take turns breaking a chacolate bar (rectangle-shaped consist of squares). The last to break a piece wins the game.

Design the strategy.

Solution

Each time the bar is broken, total number of pieces increase by 1. Suppose there’re even number of squares, 1st player wins regardless of breaking strategy. And vice versa.

Problem #2

link

75 teams took part in a competition where teams met 1-on-1. Each time the defeated team drops out.

How many meets are needed to before one team is declared a winner?

Solution

Each game will eliminate 1 game, so it needs 74 games.

Splitting Piles

link

Given a random number of items in a pile. Ask an audience to split a pile into two piles, multiply the numbers of items in the two new piles and keep adding the results. The process stops when there is no pile with more than 1 chip.

For example, let start with 9 chips:

PilesWhich is brokenWhat's addedTotal

993*618
3,631*220
1,2,663*329
1,2,3,331*231
1,2,1,2,321*132
1,1,1,1,2,321*133
1,1,1,1,1,1,331*235
1,1,1,1,1,1,1,221*136
1,1,1,1,1,1,1,1,1---

Before the audience told the final number, you immediately guess it’s 36. How did you do it?

Solution

The result does not depend on how the piles are split; but only on the initial size of the very first pile. Answer is always N(N - 1)/2.

This can be proved by mathematical induction.