Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: Longest increasing subsequence in O(n log n) with a 'tails' array (tails[i] = smallest possible tail of an increasing subsequence of length i + 1).
Problem: Longest increasing subsequence in O(n log n) with a 'tails' array (tails[i] = smallest possible tail of an increasing subsequence of length i + 1).
from bisect import bisect_left
def tails_of(a):
t = []
for x in a:
i = bisect_left(t, x)
if i == len(t): t.append(x)
else: t[i] = x
return t
print(tails_of([3, 10, 2, 1, 20, 4, 6, 7]))What is printed?- A. [1, 4, 6, 7]
- B. [3, 4, 6, 7]
- C. [1, 10, 20]
- D. [2, 4, 6, 7]
Correct answer: A. [1, 4, 6, 7]
Trace: [3] → [3,10] → [2,10] → [1,10] → [1,10,20] → [1,4,20] → [1,4,6] → [1,4,6,7]. Each value replaces the first tail ≥ it, so only the length (4) is guaranteed meaningful; the array is not in general a real subsequence.
More Coding Interview Problems questions
- Problem: Return the length of the shortest string that contains both a and b as subsequences. python def scs(a, b): m, n = len(a), len(b)…
- Problem: The reflected binary Gray code of i is i ^ (i >> 1). At which position of the 3-bit Gray sequence does the code 6 (110) appear?…
- Problem: A permissions engine isolates bits of a mask. python x = 88 print(x & -x, x & (x - 1)) What is printed?
- Problem: Rearrange the digits of a version list into the next lexicographically greater permutation. python def nxt(a): i = len(a) - 2…
- An analytics service holds a static array of n = 10⁵ integers and must answer q = 10⁶ queries 'minimum of a[l..r]' as fast as possible…
- Given n = 5 × 10⁴ integers, count pairs i < j with a[i] > 2·a[j]. Which approach is correct and efficient?