Solve parsing, counting, ranges, and window problems
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This set of problems evaluates parsing and string-processing, array and range reasoning, binary search, and sliding-window/frequency-counting competencies within the Coding & Algorithms domain, covering expression evaluation with operator precedence, counting occurrences in sorted arrays, summarizing missing ranges, and minimum-window substring problems. Such questions are commonly asked to assess the ability to implement efficient algorithms that meet specified time and space bounds while handling edge cases, and they require practical implementation skills (linear and logarithmic time techniques) combined with conceptual understanding of precedence, search boundaries, and two-pointer/window strategies.
Expression Evaluator with + and *
Constraints
- 1 <= s.length <= 1e5
- s contains only digits, '+', '*', spaces, and commas
- The input expression is valid
- All intermediate results fit in a 64-bit signed integer
Examples
Input: ("2+3*4",)
Expected Output: 14
Explanation: 3*4 = 12, then 2 + 12 = 14 (multiplication first).
Input: ("1, 2, 3 * 4",)
Expected Output: 15
Explanation: Commas and spaces are separators: 1 + 2 + (3*4) = 1 + 2 + 12 = 15.
Input: ("0",)
Expected Output: 0
Explanation: A single number evaluates to itself.
Input: ("3*3*3+1",)
Expected Output: 28
Explanation: 3*3*3 = 27, then 27 + 1 = 28.
Input: ("10 + 20 + 30",)
Expected Output: 60
Explanation: Pure addition: 10 + 20 + 30 = 60.
Input: ("100*0+5",)
Expected Output: 5
Explanation: 100*0 = 0, then 0 + 5 = 5.
Hints
- Use a single left-to-right pass. Track a running `total` (committed sum) and a `cur` value representing the current multiplicative term.
- When you see a '+', commit `cur` into `total` and start a new term with the next number. When you see a '*', multiply the next number into `cur`.
- Initialize `cur = 0` and treat the first operator as '+'. Skip spaces and commas as token separators.
Count Target Occurrences in a Sorted Array
Constraints
- 0 <= len(nums) <= 1e5
- nums is sorted in nondecreasing order
- -1e9 <= nums[i], target <= 1e9
Examples
Input: ([5,7,7,8,8,10], 8)
Expected Output: 2
Explanation: 8 appears at indices 3 and 4, so the count is 2.
Input: ([5,7,7,8,8,10], 6)
Expected Output: 0
Explanation: 6 is not in the array.
Input: ([], 0)
Expected Output: 0
Explanation: An empty array contains no occurrences.
Input: ([1,1,1,1,1], 1)
Expected Output: 5
Explanation: Every element equals the target.
Input: ([-3,-3,-1,0,2], -3)
Expected Output: 2
Explanation: -3 appears at indices 0 and 1 (negatives handled correctly).
Input: ([2], 2)
Expected Output: 1
Explanation: Single-element array containing the target.
Input: ([1,2,3,4,5], 6)
Expected Output: 0
Explanation: Target greater than every element.
Hints
- The count equals (index of last occurrence) - (index of first occurrence) + 1, or 0 if absent.
- Find the first index where nums[i] >= target (lower bound) and the first index where nums[i] > target (upper bound). The difference is the count.
- Use half-open binary search [lo, hi) to avoid off-by-one errors; the count is upper_bound - lower_bound.
Summarize Missing Ranges within [0, 99]
Constraints
- 0 <= len(present) <= 100
- Each value in present is in [0, 99]
- present is sorted and contains unique values
Examples
Input: ([0,1,5,9,99],)
Expected Output: ['2->4', '6->8', '10->98']
Explanation: Missing runs: 2-4 (len 3 -> range), 6-8 (len 3 -> range), 10-98 (len 89 -> range).
Input: ([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],)
Expected Output: []
Explanation: Every value in [0, 99] is present, so nothing is missing.
Input: ([],)
Expected Output: ['0->99']
Explanation: Everything is missing: one run 0-99 (len 100 -> range).
Input: ([0,3,99],)
Expected Output: ['1,2', '4->98']
Explanation: Missing 1-2 (len 2 -> individual '1,2') and 4-98 (len 95 -> range).
Input: ([5],)
Expected Output: ['0->4', '6->99']
Explanation: Missing 0-4 (len 5 -> range) and 6-99 (len 94 -> range).
Input: ([0,1,2,3,4,5,6,7,8,9,11,99],)
Expected Output: ['10', '12->98']
Explanation: Missing 10 (len 1 -> individual '10') and 12-98 (len 87 -> range).
Hints
- Build a set of present values, then scan i from 0 to 99 to find maximal runs of consecutive missing integers.
- For each missing run [a, b], compute its length = b - a + 1.
- If the run length is >= 3, emit "a->b". Otherwise emit each missing number, comma-joined (a single missing number emits just its value as a string).
Minimum Window Covering a Multiset
Constraints
- 0 <= len(s), len(t) <= 1e5
- s and t consist of arbitrary characters
- If t is empty, return the empty string
Examples
Input: ("ADOBECODEBANC", "ABC")
Expected Output: 'BANC'
Explanation: BANC is the shortest substring containing A, B, and C.
Input: ("a", "a")
Expected Output: 'a'
Explanation: The whole string is the minimal window.
Input: ("a", "aa")
Expected Output: ''
Explanation: s has only one 'a' but t needs two, so no valid window.
Input: ("", "a")
Expected Output: ''
Explanation: Empty source cannot cover a non-empty target.
Input: ("aaflslflsslf", "")
Expected Output: ''
Explanation: Empty target -> empty string by definition.
Input: ("aaaaaaaaaab", "aab")
Expected Output: 'aab'
Explanation: Shortest window needs 2 a's and 1 b; the trailing 'aab' (last two a's + b) is minimal.
Input: ("abc", "cba")
Expected Output: 'abc'
Explanation: All three characters are required; the whole string is the only window.
Hints
- Build a frequency map `need` for t. Expand a right pointer, maintaining a `window` frequency map for the current substring of s.
- Track how many distinct required characters have reached their needed multiplicity (`formed`). When formed equals the number of distinct characters in t, the window is valid.
- While the window is valid, record it if it's the shortest so far, then shrink from the left to try to find a smaller valid window.