Solve Data-Structure Problems in Python Interview Round
Company: Meta
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in data-structure manipulation and aggregation in Python, covering digit/string processing, frequency counting with group-level deduplication, and interval-based summation across records.
Smallest Integer From Odd Digits
Constraints
- n fits in a signed 32-bit integer.
- Only digits 1, 3, 5, 7, 9 are considered odd.
- Return -1 if the number has no odd digits.
- The sign of n does not affect the result.
Examples
Input: (12345,)
Expected Output: 135
Explanation: Odd digits 1,3,5 sorted ascending form 135.
Input: (2468,)
Expected Output: -1
Explanation: No odd digits, so -1.
Input: (975312468,)
Expected Output: 13579
Explanation: Odd digits 9,7,5,3,1 sorted ascending form 13579.
Input: (0,)
Expected Output: -1
Explanation: 0 is even; no odd digits, so -1.
Input: (7,)
Expected Output: 7
Explanation: Single odd digit returns itself.
Input: (-531,)
Expected Output: 135
Explanation: Sign is ignored; odd digits 5,3,1 sorted form 135.
Input: (113355,)
Expected Output: 113355
Explanation: All digits already odd; sorted ascending stays 113355.
Hints
- Iterate over the decimal digits of |n| and keep only the odd ones.
- Sorting the odd digits ascending yields the smallest possible arrangement.
- Handle the empty case (no odd digits) by returning -1.
Most Frequent Comments Across Stores
Constraints
- Comments are compared by exact string equality.
- Within a store, duplicate comments count as one.
- Ties are all returned, sorted ascending.
- Return [] for an empty input dictionary.
Examples
Input: ({'A': ['good', 'good', 'bad'], 'B': ['good', 'ok'], 'C': ['good', 'bad']},)
Expected Output: ['good']
Explanation: 'good' is in all 3 stores (its repeat in A counts once); 'bad' only 2; 'ok' only 1.
Input: ({'A': ['x', 'x', 'x'], 'B': ['y'], 'C': ['z']},)
Expected Output: ['x', 'y', 'z']
Explanation: After per-store dedup each comment appears in exactly 1 store, so all tie at count 1.
Input: ({},)
Expected Output: []
Explanation: No stores -> empty result.
Input: ({'Solo': ['repeat', 'repeat', 'repeat', 'unique']},)
Expected Output: ['repeat', 'unique']
Explanation: In one store, 'repeat' dedups to count 1, same as 'unique'; both tie.
Input: ({'A': ['p', 'q'], 'B': ['q', 'r'], 'C': ['r', 'p']},)
Expected Output: ['p', 'q', 'r']
Explanation: Each of p, q, r appears in exactly 2 stores, all tied at count 2.
Input: ({'A': ['hello'], 'B': ['hello'], 'C': ['world']},)
Expected Output: ['hello']
Explanation: 'hello' is in 2 stores, 'world' in 1; 'hello' wins.
Hints
- Convert each store's comment list to a set to deduplicate within the store.
- Accumulate a global Counter over the deduped per-store comments.
- Find the maximum count, then return every comment achieving it, sorted.
Maximum Class Total Over Two Consecutive Years
Constraints
- Each class contributes its number_of_classes to every year in [start_year, end_year] inclusive.
- start_year <= end_year for every class.
- A two-consecutive-year window is (y, y+1); a lone busiest year (paired with an empty year) is also valid.
- Return 0 when the list is empty.
Examples
Input: ([['Math', 3, 2020, 2021], ['Science', 2, 2021, 2022]],)
Expected Output: 8
Explanation: Year totals 2020:3, 2021:5, 2022:2; best pair 2020+2021 = 8.
Input: ([['Solo', 5, 2020, 2020]],)
Expected Output: 5
Explanation: Only 2020 is active with total 5; no following year, so the answer is 5.
Input: ([],)
Expected Output: 0
Explanation: No classes -> 0.
Input: ([['A', 4, 2020, 2020], ['B', 6, 2021, 2021]],)
Expected Output: 10
Explanation: 2020:4 and 2021:6 are consecutive, combined 10.
Input: ([['A', 1, 2020, 2020], ['B', 1, 2025, 2025]],)
Expected Output: 1
Explanation: Years 2020 and 2025 are not consecutive; best window is a single year = 1.
Input: ([['Long', 2, 2018, 2022]],)
Expected Output: 4
Explanation: Each of 2018..2022 has total 2; any consecutive pair sums to 4.
Input: ([['A', 3, 2020, 2021], ['B', 3, 2020, 2021], ['C', 10, 2020, 2020]],)
Expected Output: 22
Explanation: 2020: 3+3+10 = 16, 2021: 3+3 = 6; best pair 16+6 = 22.
Hints
- Expand each class into the years it spans and accumulate per-year totals.
- Scan years in order; for each year y, the window value is total[y] + total[y+1].
- Initialize the answer with the busiest single year so a one-year span is covered.