Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the minimum number of operations to make the array [5, 1, 4] strictly increasing where one operation increments any element by 1? Run the greedy code.
Problem: What is the minimum number of operations to make the array [5, 1, 4] strictly increasing where one operation increments any element by 1? Run the greedy code.
a = [5, 1, 4]
ops = 0
for i in range(1, len(a)):
if a[i] <= a[i - 1]:
ops += a[i - 1] + 1 - a[i]
a[i] = a[i - 1] + 1
print(ops)What is printed?- A. 5
- B. 3
- C. 4
- D. 8
Correct answer: D. 8
5 stays, 1 must become 6 (5 ops), then 4 must become 7 (3 ops), total 8. Allowing equal neighbours gives 5, overwriting ops instead of adding gives 3, and summing the original drops gives 4.
More Coding Interview Problems questions
- Problem: What is the number of integers in [1, 999] whose digit product is 0? Run the code. python print(sum(1 for x in range(1, 1000) if…
- Problem: How many distinct pairs (i < j) of [6, 10, 15, 21] have gcd(ai, aj) = 1? Run the code. python from math import gcd a = [6, 10…
- Problem: What is the smallest positive integer that is divisible by every integer from 1 to 12? Run the code. python from math import lcm…
- Problem: Which technique gives the number of distinct subarrays of an array of length n in O(n log n) time using a suffix structure?
- Problem: Which statement about the Knuth optimization for interval DP is correct when its monotonicity conditions hold?
- Problem: What is the time complexity of building a suffix array with prefix doubling and radix sort?