Hackernoon logoSolve TED-Ed’s Frog Riddle with JavaScipt by@okaybenji

Solve TED-Ed’s Frog Riddle with JavaScipt

Author profile picture

@okaybenjiBenji Kay

Photo by Roberto Lopez (https://unsplash.com/photos/vxKyz3HLw2g)

TED-Ed is a YouTube channel with educational content. They occasionally post riddles, give viewers a chance to solve them, and then explain how they work. One such quandary is the “frog riddle”, and its solution has proven a bit controversial! It’s pretty short, why don’t you go ahead and watch it?

See if you can solve the riddle on your own

So we’ve gone and poisoned ourselves! And we can only administer the antidote by licking a female frog of a particular species. The male and female look the same, but only the male croaks.

Given the choice between a single, non-croaking frog on one side or a pair of frogs, one of whom we heard croak, on the other, which option gives us the best odds for survival?

We are told that licking both frogs in the pair gives us much better chances. This idea is certainly counter-intuitive! But we can demonstrate its principle pretty simply with a bit of JavaScript.

Disclaimer: I’m no mathematician. I’m just a coder. If you notice any mistakes, please let me know in the comments.

Let’s run a simulation that will pick sexes of frogs at random and see what the distribution looks like. We’ll run the simulation a million times to ensure statistically significant results.

Here, we’re representing our frog pairs as strings. As you can see, if we pull pairs of frogs at random, the distribution is pretty even between:

  • female-male pairs with the female on the left,
  • female-male pairs with the female on the right,
  • pairs where both frogs are female, and
  • pairs where both frogs are male.

We don’t really care about the order of the frogs at this point, so we could simplify our odds as follows:

Female-Male: 50%

Female-Female: 25%

Male-Male: 25%

But we know from the riddle that the pair behind us definitely contains a male, so we can toss out the possibility of a female-female pair. We could certainly do this in our heads, but let’s re-write the code to just solve the riddle for us.

And thus our new odds are:

Female-Male: 67%

Male-Male: 33%

Since the chances of the lone frog being female are 50/50, it’s clear that our odds of encountering a female are better if we lick both frogs in the pair.

So we’ve solved the riddle as it was described, and our solution agrees with TED-Ed. If you’re satisfied, the story can end here.

…But one point of contention is that the riddle specifically says that you don’t know which frog in the pair croaked. Is this detail important? Some people seem to think so!

I encountered the Frog Riddle via this video on Shaun’s YouTube channel, which questions whether TED-Ed’s riddle itself isn’t wrong.

What indeed

While I do want to address that question, first I’d like to address an idea I found in its comments section:

Woah, what? Now this idea certainly is counter-intuitive. Commenters have suggested that by observing which of the two frogs croaked, we’ve now changed the odds of survival.

I would certainly agree that the odds you calculate can change as you acquire more information, but let’s think about this case in particular, using our intuition.

Let’s assume for now that Liam is correct, and our odds will become 50/50 upon observing the particular frog who croaks. Here are the two possibilities.

  1. You see the frog on the left croak, and your odds of survival are now 50%.
  2. You see the frog on the right croak, and your odds of survival are now 50%.

The important thing to note here is the odds of events 1 and 2 occuring are the same — also 50/50 — regardless of our odds of survival. If all of this is true, it doesn’t matter which frog croaks. Even before we see it croak, we can be sure our odds of survival are evenly split between either 50% or 50%!

However, we’ve proven our odds sit at about 67%, so something’s gotta give. Let’s get to the bottom of this with a bit more JS. Let’s find out for sure what our odds are if we know that the frog on the left croaked.

Since we need to know which of our frogs has croaked, we’re no longer using strings to represent them. Frogs are now represented as objects with a sex property and an optional croaked property. We’re tossing out not just pairs where both frogs are female, but also pairs where the frog that croaked is on the right.

We can see that even when it’s always the frog on the left that croaks, we still have female frogs in twice as many pairs!

This also makes sense if we lay out the sample space. Before eliminating pairs where the frog on the right croaked, the space looked like this:

Mc M M Mc Mc F × 2 • F Mc × 2

Where Mc is a male frog who croaked. After eliminating pairs where the frog on the right croaked, it looks like this:

Mc M Mc F × 2

We cut the number of pairs containing females in half, but so too did we cut in half the number of pairs containing only males. Thus, our odds are unchanged.

Finally, I want to address the idea presented in Shaun’s video that the riddle is fundamentally flawed. This problem, I think, requires a more philosophical approach, so I’ll be setting JS aside.

The argument is based on the idea that a pair of male frogs would produce twice as many croaks as a pair of frogs with only one male. I get this argument! But it adds more assumptions to the problem than were presented. In a real life-or-death situation, it might make sense to extrapolate from your own assumptions about biology to try to make your decision about which frog to lick.

However, if you’re trying to solve this riddle, it’s probably best to rely only on the information TED-Ed has given you. For all we know, frogs only croak when in pairs, and only one frog in a pair will assume the role of croaker. You might think this would be less likely than a pair of male frogs croaking more frequently than a single frog, but you’d only be guessing.

Well, that’s all I’ve got. What do you think? Have I convinced you? If not, feel free to share some code of your own to prove your point!

You can follow me on Twitter @okaybenji


Join Hacker Noon

Create your free account to unlock your custom reading experience.