Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: Find the length of the longest strictly increasing subsequence of daily sales.
Problem: Find the length of the longest strictly increasing subsequence of daily sales.
from bisect import bisect_left
def lis(a):
tails = []
for x in a:
i = bisect_left(tails, x)
if i == len(tails): tails.append(x)
else: tails[i] = x
return len(tails), tails
print(lis([10, 9, 2, 5, 3, 7, 101, 18, 4, 19]))What is printed?- A. (5, [2, 3, 7, 18, 19])
- B. (5, [2, 3, 4, 18, 19])
- C. (4, [2, 3, 7, 18])
- D. (6, [2, 3, 4, 7, 18, 19])
Correct answer: B. (5, [2, 3, 4, 18, 19])
tails[k] is the smallest possible tail of an increasing subsequence of length k + 1; each element replaces the first tail ≥ it. The length 5 is correct (e.g. 2, 3, 7, 18, 19), but the tails array itself, [2, 3, 4, 18, 19], is NOT a real subsequence – 4 comes after 18 in the input. O(n log n).
More Coding Interview Problems questions
- Problem: Find the length of the longest common subsequence of two version strings. python def lcs(a, b): dp = [[0] (len(b) + 1) for in…
- Problem: Find the minimum number of insert, delete or replace operations to convert one word to another (edit distance). python def…
- Problem: Count paths from the top-left to the bottom-right of a grid moving only right or down, where 1 marks a blocked cell. python def…
- Problem: Find the minimum path sum from the top-left to the bottom-right of a cost grid (moves: right or down). python def minpath(g): R…
- Problem: Count the ways to decode a digit string where 'A' = 1 … 'Z' = 26. python def decode(s): prev2, prev1 = 1, (1 if s[0] != '0' else…
- Problem: Decide whether a string can be split into words from a dictionary. python def wb(s, words): W = set(words); dp = [True] + [False]…