Data Structures & AlgorithmsHardICPC regional · Product Company SDE
Problem: The longest common subsequence (LCS) of two strings of length n and m is typically solved with which approach and complexity?
- A. A single greedy pass, O(n+m)
- B. Bottom-up DP over an (n+1)x(m+1) table, O(nm) time and space
- C. Sorting both strings, O(n log n + m log m)
- D. Brute-force over all subsequences, O(2^n)
Correct answer: B. Bottom-up DP over an (n+1)x(m+1) table, O(nm) time and space
The classic LCS recurrence compares characters and takes the best of three sub-results, filled bottom-up in an (n+1)x(m+1) table: O(nm) time and space (space can be reduced to O(min(n,m)) with rolling rows).
More Coding Interview Problems questions
- Problem: Subset-sum (does any subset of n integers sum exactly to a target T?) has a DP solution running in O(nT). Why is this not…
- Problem: What is the average-case and worst-case time complexity of the quickselect algorithm for finding the k-th smallest element?
- Problem: Which of these sorting algorithms is guaranteed O(n log n) in the worst case AND is stable (preserves the relative order of equal…
- Problem: Counting sort can sort n integers in the range [0, k] in O(n + k) time. What is the key limitation that makes it unsuitable for…
- Problem: A trie (prefix tree) storing n words with total length L supports which operation's time complexity most efficiently?
- Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?