in 1995 i bought a computer with a plan to install something other than microsoft on it. i bought a book on Linux that had a cd in an envelope glued inside the back cover. it ran off the cd, which made it painfully slow. and when you ejected the cd it went back to Windows 95. next thing i tried was Slackware. everything is a tarball -- at least it was quicker than the cd. i blew away Win95 for Slackware. and then stumbled on Debian -- it had a package manager woo hoo! but by about 2002 or so, i was just running whatever came with the computer, since it was evident that everyone was not going to do what desperately needed to be done, and dump mickeysoft.
so this operating system apathy persisted until some time into the 2010s, about the time systemd and codes of conduct were being forced onto every distro. i knew civilization was crashing but the wreckage of open source software has been a shocking thing to behold.
<amateur-psychiatry>he suffered from bipolar disorder, and was undiagnosed and unmedicated. </amateur-psychiatry> sometimes they do the most amazing midnight code projects, but they can be hard to live with. I did not pay that much attention to him over the years, but at some point the distro started going downhill . . .
I was on twitter that evening, and saw his desperate tweets... did not know what to do, it was very sad. America, where you call the police if there's a problem, and they come shoot your dog and beat you senseless...
> Given two eggs, find the highest floor an egg can be dropped from without breaking, with as few drops as possible.
Wait, this is different from the problem statement in the image. This is just "start from the bottom and go up", the image was like "What's the worst-case number of floors you have to test?"
I like the "minimax" perspective which says that, since the question is asking about the worst-case scenario, I can imagine that I am playing against an adversary who decides, at each step, whether my egg cracks or survives. (Of course, his decisions must be self-consistent.)
Suppose I choose to drop my first egg at the midpoint N/2. I will be strictly better off in the world where my egg *doesn't* break because I'll have the same number of floors (N/2 - 1) to rule out but one more egg to do it with. Therefore, the adversary will choose to have my egg break.
In the optimal solution, the adversary should be indifferent to every choice which he is faced with. I accomplish this by dropping the first egg lower than N/2. Then, in the world where my egg "does" break, the handicap of having one less egg is balanced out by the advantage of having fewer floors to rule out. Effectively, I am buying insurance.
If the question had asked about the average scenario (w.r.t. some distribution), it would be as if the adversary doesn't play perfectly. It is still advantageous to purchase the insurance, but less insurance is needed because the adversary may fail to exploit my weakness. This intuition tells you that the optimal strategy for the average scenario would involve dropping each egg at a point higher than the "worst case" answer but still lower than N/2.
---
Another perspective is that an answer such as "binary search until the last egg, and then linear search" should set off warning bells because there's no reason why "1 egg" and ">1 eggs" should have such a stark difference.
If there are a lot of eggs (specifically, > log_2(N)), then you obviously do binary search. If there's only 1 egg, then you have to do linear search. It makes sense that the general answer would smoothly interpolate between these two regimes, which it does.
In fact, for E eggs, the number of steps required is O(N^(1/E)), and this order of growth does converge to O(log N) in the limit E -> infinity. (Related fact: geometric mean is the power mean for exponent p = 0, as explained here: https://en.wikipedia.org/wiki/Generalized_mean )
Think about it concretely, like in the linked post:
If you have two eggs and 100 floors and you binary search, and the egg cracks on the 49th floor, the binary search makes you check 50 times.
If you have two eggs and you use your first egg to check every 10th floor starting from the bottom, and the egg cracks on the 99th floor, this makes you check 19 times. Which is better.
You benefit from skewing the partition by making the "oh fuck I'm out of eggs" scenario less bad. This makes the problem "how do we optimize this, if we're allowed so many eggs"?
For any M, define S(M, E) to be the number of ways to choose at most E elements from a set of size M. In other words, it's the sum of the first (E+1) numbers in the (M+1)-st row of Pascal's Triangle.
The answer is the smallest M such that S(M, E) > N.
To see why S(M, E) shows up, consider the possible outcomes of performing M drops. Each outcome can be summarized by saying, for each drop, whether a crack happened at that drop. Since we only have E eggs, there can be at most E cracks, so the set of possible outcomes has size at most S(M, E).
Since there are N+1 possible answers, we certainly need M to be big enough such that S(M, E) >= N+1. This shows that the claimed answer is a lower bound.
To show that it's an upper bound, we have to construct a strategy. The key is to observe that S(M-1, E-1) + S(M-1, E) = S(M, E) + 1, essentially by Pascal's Identity. Do the first drop from floor S(M-1, E-1). If the egg survives, then we have E eggs left and at most S(M-1, E) - 1 floors to rule out. If the egg cracks, then we have E-1 eggs left and exactly S(M-1, E-1) - 1 floors to rule out. In either case, we can proceed inductively.
@ceo_of_monoeye_dating@ai@ExtraSpecialK@MercurialBlack@meso@mia@p@scenesbycolleen@shibao amusingly enough what actually got me was comprehending the scenario itself. I failed to notice that the depth to which the partitioning search runs is dependent on the direction in which you branch at each step. as soon as I saw "sorted search" I apparently stopped thinking :blobfoxlaughsweat:
as a conceptual explanation I actually really like AI's scenario with the adversary
it's interesting that this property which seems somewhat unusual from the perspective of a search algorithm running on a dataset that fits within RAM is probably the norm in the physical realm (most irl scenarios have severely non-uniform cost functions, resources are usually severely limited, success is often not guaranteed)
but learning that it's been used as an interview question I have to wonder what candidate characteristics the problem was actually filtering for. in a situation where someone is expecting you to pose scenarios with which to quickly demonstrate some basic proficiency I think they're quite likely to miss unusual details and I'm not sure what catching that is supposed to accomplish
:blobcatsuit: aHA! you *thought* I wanted you to demonstrate inverting a binary tree but you were in fact mistaken!
:gura_pain: so then if you hire me, can I expect such confusing communication of requirements to be a regular thing?