Data Structures & AlgorithmsVery hardICPC regional · DP puzzles
Problem: You have 2 eggs and a building with 10 floors. What is the minimum number of drops needed in the worst case to find the highest safe floor? Run the code.
Problem: You have 2 eggs and a building with 10 floors. What is the minimum number of drops needed in the worst case to find the highest safe floor? Run the code.
floors = 10
m = 0
while m * (m + 1) // 2 < floors:
m += 1
print(m)What is printed?- A. 3
- B. 5
- C. 10
- D. 4
Correct answer: D. 4
With 2 eggs and m drops you can distinguish m(m+1)/2 floors, and 4 * 5 / 2 = 10 is the first value at least 10. Binary search (needing a broken egg per halving) is the trap.
More Coding Interview Problems questions
- Problem: How many distinct ways can the string 'leetcode' be segmented into words from the dictionary {'leet', 'code', 'leetcode', 'lee'…
- Problem: Count the connected components of the undirected graph on 6 vertices with edges (0,1), (1,2), (3,4), (5,5) using DFS. Run the…
- Problem: Check whether the directed graph with edges 0->1, 1->2, 2->0, 2->3 contains a cycle using Kahn's algorithm (count the nodes that…
- Problem: Find the minimum cost to reach node 3 from node 0 using at most 2 edges in the directed graph with edges (0,1,5), (1,3,5)…
- Problem: Count the nodes at depth exactly 2 in the rooted tree with children 0: [1, 2], 1: [3], 2: [4, 5], 3: [], 4: [], 5: []. Root 0 has…
- Problem: How many integers from 1 to 1000 are divisible by 3 or by 5 but not by both?