Solve prefix, array, and string problems
Company: Pinterest
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This set of problems evaluates proficiency in string and array algorithm design—covering prefix search, subsequence matching, and maximal-area computation from bar heights—measuring competence in algorithmic thinking, data structure usage, and time/space efficiency.
Part 1: First index matching a prefix
Constraints
- 0 <= len(words) <= 200000
- words is sorted in non-decreasing lexicographic order
- 0 <= len(prefix) <= 100000
- Each word contains lowercase English letters
Examples
Input: (["a", "apple", "appz", "b"], "ap")
Expected Output: 1
Explanation: "apple" is the first word that starts with "ap".
Input: (["banana"], "ban")
Expected Output: 0
Explanation: The only word matches the prefix.
Input: (["ant", "bat", "cat"], "car")
Expected Output: -1
Explanation: No word starts with "car".
Input: ([], "a")
Expected Output: -1
Explanation: An empty list has no matching index.
Input: (["a", "ab", "b"], "")
Expected Output: 0
Explanation: Every string starts with the empty prefix, so the first index is 0.
Hints
- Because the array is sorted, you can search for the first position where a word is not less than the prefix.
- After binary search, verify that the candidate actually starts with the prefix.
Part 2: Maximum rectangle from bar heights
Constraints
- 0 <= len(heights) <= 200000
- 0 <= heights[i] <= 1000000000
- Bars are adjacent and each bar has width 1
Examples
Input: [2, 1, 5, 6, 2, 3]
Expected Output: 10
Explanation: The best rectangle uses heights 5 and 6 with width 2, giving area 10.
Input: [2, 4]
Expected Output: 4
Explanation: The best area is max(2*2, 4*1) = 4.
Input: []
Expected Output: 0
Explanation: No bars means no rectangle.
Input: [6]
Expected Output: 6
Explanation: A single bar forms a rectangle of area 6.
Input: [0, 2, 0]
Expected Output: 2
Explanation: Only the middle bar contributes a positive area.
Hints
- For each bar, imagine extending left and right until you hit a shorter bar.
- A monotonic increasing stack can help compute the best area in one pass.
Part 3: Check ordered deletion match
Constraints
- 0 <= len(s), len(t) <= 200000
- Strings may contain any characters
- Character comparison is case-sensitive
Examples
Input: ("ace", "abcde")
Expected Output: True
Explanation: Delete 'b' and 'd' from t to get "ace".
Input: ("aec", "abcde")
Expected Output: False
Explanation: The needed order does not appear in t.
Input: ("", "anything")
Expected Output: True
Explanation: The empty string can always be formed by deleting all characters.
Input: ("a", "")
Expected Output: False
Explanation: A non-empty string cannot be formed from an empty string.
Input: ("abc", "abc")
Expected Output: True
Explanation: No deletions are needed when the strings already match.
Hints
- Walk through t from left to right while tracking how many characters of s you have matched.
- If you match all of s in order, the answer is True even if t still has characters left.