Could you pass this interview? The famous batteries and flashlight logic puzzle

124,028
0
Published 2024-07-24
This is a famous problem that has been used in technical interviews, and it has even appeared on a Mathematical Olympiad. There are 4 good batteries and 4 bad batteries, and you need to insert 2 good batteries to make a flashlight work. How many tests do you need?

0:00 problem
1:53 estimate
5:18 strategy 1
7:47 strategy 2
10:46 solution
13:08 proof

Bogumił Kamiński
bkamins.github.io/julialang/2023/06/23/graphspuzzl…
Brazil Olympiad
imomath.com/othercomp/Bra/BraMO05.pdf
StackExchange
math.stackexchange.com/questions/3323740/brazilian…
math.stackexchange.com/questions/3115018/we-have-n…
puzzling.stackexchange.com/questions/108023/flashl…
Mike Pawliuk
mikepawliuk.ca/2012/09/06/the-battery-problem-math…
Glassdoor
www.glassdoor.com/Interview/Round-4-It-was-a-manag…
LogicallyYours
   • Torch and 8 Batteries Puzzle || Think...  

Subscribe: youtube.com/user/MindYourDecisions?sub_confirmatio…

Send me suggestions by email (address at end of many videos). I may not reply but I do consider all ideas!

If you purchase through these links, I may be compensated for purchases made on Amazon. As an Amazon Associate I earn from qualifying purchases. This does not affect the price you pay.

If you purchase through these links, I may be compensated for purchases made on Amazon. As an Amazon Associate I earn from qualifying purchases. This does not affect the price you pay.

Book ratings are from January 2023.

My Books (worldwide links)
mindyourdecisions.com/blog/my-books/#worldwide

My Books (US links)
Mind Your Decisions: Five Book Compilation
amzn.to/2pbJ4wR
A collection of 5 books:
"The Joy of Game Theory" rated 4.3/5 stars on 290 reviews
amzn.to/1uQvA20
"The Irrationality Illusion: How To Make Smart Decisions And Overcome Bias" rated 4.1/5 stars on 33 reviews
amzn.to/1o3FaAg
"40 Paradoxes in Logic, Probability, and Game Theory" rated 4.2/5 stars on 54 reviews
amzn.to/1LOCI4U
"The Best Mental Math Tricks" rated 4.3/5 stars on 116 reviews
amzn.to/18maAdo
"Multiply Numbers By Drawing Lines" rated 4.4/5 stars on 37 reviews
amzn.to/XRm7M4

Mind Your Puzzles: Collection Of Volumes 1 To 3
amzn.to/2mMdrJr
A collection of 3 books:
"Math Puzzles Volume 1" rated 4.4/5 stars on 112 reviews
amzn.to/1GhUUSH
"Math Puzzles Volume 2" rated 4.2/5 stars on 33 reviews
amzn.to/1NKbyCs
"Math Puzzles Volume 3" rated 4.2/5 stars on 29 reviews
amzn.to/1NKbGlp

2017 Shorty Awards Nominee. Mind Your Decisions was nominated in the STEM category (Science, Technology, Engineering, and Math) along with eventual winner Bill Nye; finalists Adam Savage, Dr. Sandra Lee, Simone Giertz, Tim Peake, Unbox Therapy; and other nominees Elon Musk, Gizmoslip, Hope Jahren, Life Noggin, and Nerdwriter.

My Blog
mindyourdecisions.com/blog/

Twitter
twitter.com/preshtalwalkar

Instagram
www.instagram.com/preshtalwalkar/

Merch
teespring.com/stores/mind-you...

Patreon
www.patreon.com/mindyourdecisions

Press
mindyourdecisions.com/blog/press

All Comments (21)
  • @commontater652
    If my boss won't let me use my multi-meter I need a new job.
  • @doq
    If your battery supplier has a defect rate of 50% you might want to consider a different supplier. 7 worst case btw.
  • @Parlik
    The first example did more tests than was needed with that method. After testing 1 with 2 to 6, and the flashlight didn't turn on, there is no need to test with 7 and 8, as you must have tested 1 with at least one good battery in the other slot, and since the flashlight didn't turn on, 1 must be bad.
  • @mickyderheld
    missed the opportunity to call half of them baderies
  • Solution: lick finger, hold onto negative terminal, lick positive terminal... if you can feel tingle, it's a good Battery.
  • @verkuilb
    Duracell gonna sue for using their trademarked color scheme and saying half their batteries are bad. 😂
  • @shanehebert396
    Try dropping the batteries and seeing if they bounce ;)
  • @Woztek10
    Another solution is dividing the group in 3+5. If the pairs of the 1st group (3) are off that means worst case there is one good and others bad and the group of 5 has 2 bad and 3 good batteries (total 3 tries). Then take the 2nd group and do 2 pairs (4+5 and 6+7), both are off means both the subgroups have one good and one bad battery and the 8th one is good (total 5 tries). Now take the 8 and pair with 4 and 5 (total 7 tries).
  • @howareyou4400
    The optimal construct can be summarized using elementary school Olympiad technics: pigeonhole principal. We have 4 good batteries split into 3 groups of size 3, 3, 2, then at least one group has a pair of good batteries. Then we check every pairs inside each group, that's total 7 pairs to check.
  • @KiraAkaike
    i thought about the second method because i simply don't have the braincells for the other cases
  • @Byrnage
    In the first example the maximum number of tests should be 15 i think. After 5 tests you will know if your first battery is good or not. You will have to have hit at least one good battery at that point and if the flashlight never turned on then your first battery is clearly dead. There is no need to continue testing that battery.
  • @peter_castle
    14:20 The proof has a gap that I'm sure is fillable, but should be filled: It assumes there is a group of 3 completely disconnected, the group that he "WLOG" chooses 1,2,3. Instead, he should say that this group should exist because: there 8 choose 3=56 groups of three nodes. Each edge can add to 6 groups, when you associate it with the other 6 nodes. So with 6 edges, one can add 6*6=36. However, then one group of 3 must sum less than 1, so that's the disconnected group. In fact, this argument would work with 9 edges, still there has to be a group of 3 disconnected, since 9*6=54<7*8=56.
  • @inplfw
    My gut led me to 8 tests Presh presented as solution 2. I don't think I would've gotten the 7 test solution if thought about it longer. Interesting problem.
  • Around the 14:30 mark, there seems to be a jump in reasoning. It's stated that there is a group with at most 3 nodes with no edges between them. We then continue for the proof by assuming that there is a group with exactly 3 nodes with no edges between them. Note that this can easily be remedied but in the video this step seems a bit off.
  • @3057luis
    We have 28 different ways of combining 8 batteries, 2 by 2, and 6 ways of combining 4. So 22 is the difference. +1 the last test and this gives the same answer 23 to the worst random picking cenario. Now a differnt problem is to optimise this number, making decisions based on the tests already done. Interesting problem, no doubt.
  • @noticespams6540
    As the last pair of cells in all the solutions you provided need not to be tested, as it is a certain event that the last pair will light the torch for sure.Thus the minimum no of attempts will be '6'.
  • Presh's video states and proves that this can't be done in < 7 tests. Can someone please point out the flaw in my logic? My approach was: Randomly arrange the batteries into the following groups: Pair1: 1,2 Pair2: 3,4 Triple: 5,6,7 Single: 8 Test1: Test 1-2 Test2: Test 3-4 Test3-5: Test 5-6,5-7,6-7 Worst case scenario would be if all of the above tests failed, yes? That means there must be exactly one bad battery each in pairs 1&2, and that there must be exactly 2 bad ones in the triple. If that were not the case, at least one of the tests 1-5 would have produced a good pair, no? If that's the case, then that also means that #8 must be good, because there are only 4 bad ones? Test 6: Test battery #8 (which we know is good) against one of the batteries in pair1 or pair2. Doesn't matter which one. If it works, you're done. If it fails, that means the other one in the pair must be good because there is exactly one bad one in the pair. The other one, the good one, plus #8, which we know is good gives us the good pair, no? So this can be done with only six tests? Where is the flaw in my reasoning?
  • @carlpierce2486
    What is the solution to the general problem N batteries, M of which must be in the torch for a test. With G good and B bad batteries ?
  • @pannwes
    I wrote a script to check which method is fastest on average (out of the 3 good ones), these are the results: Method 1 - 1 - 15 2 - 14 3 - 13 4 - 12 5 - 4 6 - 4 7 - 4 8 - 4 Method 2 - 1 - 15 2 - 10 3 - 6 4 - 10 5 - 6 6 - 6 7 - 9 8 - 8 Method 3 - 1 - 15 2 - 10 3 - 10 4 - 12 5 - 7 6 - 7 7 - 9 Notation: How many tries are required in how many cases, so 2 -14 means there are 14 configurations that require two tries to solve. And this is how many tries each method needs on average: Method 1 AVG - 3.34 Method 2 AVG - 4.08 Method 3 AVG - 3.61