Back to Challenge Set 19

Code Challenge: First Bad Build

Extra CreditOptional. Not required for any homework drop.

Difficulty  

divider

The Brief

What they asked for
We ship a new build every day. Build 1 worked and build 1000
is broken. Somewhere in between, something broke, and testing
one build takes ten minutes. Find the first broken build
without testing all thousand of them.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Write bool IsBroken(int build) to stand in for a real test, returning build >= 734 — then never use the number 734 anywhere else. Your search only learns by calling the test.
  • This is binary search with a twist: you are not looking for one value, you are looking for the place where working turns into broken.
  • When the middle build is broken, the first broken build is that one or an earlier one, so the middle stays in the range. Getting that edge wrong either loops forever or skips the answer.
  • Count how many times IsBroken is called, and say before you run it what the most could ever be.

Sample Output

Sample Output
First broken build: 734 (tests run: 10)

Ten tests to find one build in a thousand. Change 734 to 1, to 1000 and to 500, and check the count each time.


Submit

Your requirements list and your program file, named cc-first-bad-build.

Commence Challenge