DEV Community staff staff

Posted on

Daily Challenge #96 - Easter Egg Crush Test

Eulampy has a collection of some almost identical Fabergè eggs. One day his friend Tempter said to him:

  • Do you see that skyscraper? And can you tell me a maximal floor that if you drop your egg from will not crack it?
  • No, - said Eulampy.
  • But if you give me N eggs, - says Tempter - I'll tell you an answer.
  • Deal - said Eulampy. But I have one requirement before we start this: if I will see more than M falls of egg, my heart will be crushed instead of egg. So you have only M tries to throw eggs. Would you tell me an exact floor with this limitation?

Your task is to help Tempter - write a function

height :: Integer -> Integer -> Integer
height n m = -- see text

that takes 2 arguments - the number of eggs n and the number of trys m - you should calculate maximum skyscraper height (in floors), in which it is guaranteed to find an exactly maximal floor from which that an egg won't crack it.

Which means,

  • You can throw an egg from a specific floor every try

  • Every egg has the same, certain durability - if they're thrown from a certain floor or below, they won't crack. Otherwise, they crack.

  • You have n eggs and m tries

What is the maximum height, such that you can always determine which floor the target floor is when the target floor can be any floor between 1 to this maximum height?

height 0 14 = 0
height 2 0 = 0
height 2 14 = 105
height 7 20 = 137979

Data range
n <= 20000
m <= 20000

This challenge comes from Ivana on CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email with your suggestions!

Discussion (6)

qm3ster profile image
Mihail Malo • Edited on

I don't understand, what is the difference between a try and an egg?

Seems to me like it's

const height = (n, m) => 2 ** Math.min(n, m)

I don't understand how the example results were produced.

aminnairi profile image

You can't make mayonnaise out of a try. But you can try to make mayonnaise out of an egg (don't forget the oil and the mustard though).

qm3ster profile image
Mihail Malo

Jesus Christ

citizen428 profile image
Michael Kohl

It's a — IMHO pointless — interview question. Plenty of answers around when you google for them. It's a math problem more than a programming problem though.

qm3ster profile image
Mihail Malo • Edited on

Maybe because 🅱️inary search is 🅱️rogramming? 🤷

Edit: 🅱️ 🅱️ 🅱️ 🅱️

peledzohar profile image
Zohar Peled

I was once asked a similar question in an interview. I didn't know it back then and I can't remember it now..