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?
  1. A. (5, [2, 3, 7, 18, 19])
  2. B. (5, [2, 3, 4, 18, 19])
  3. C. (4, [2, 3, 7, 18])
  4. 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

Exams that ask this topic