Coding questions · Meta
Solve Data-Structure Problems in Python Interview Round
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.
Scenario
Interview coding round focusing on simple data-structure problems in Python.
Question
Given an integer, rearrange its digits (considering only odd digits) to create the smallest possible integer; ignore even digits entirely. 2) You are given a dictionary {store_name: [comments]}. Return the comments that appear most frequently across all stores; within a single store, count completely identical comments only once. 3) Given a list of class objects with attributes name, number_of_classes, start_year, end_year, compute the maximum total number_of_classes that occurs in any two consecutive calendar years.
Hints
Think about filtering digits, using Counter / sets for deduplication per store, and mapping yearly ranges to counts.
Smallest Integer From Odd Digits
Given an integer n, keep only its odd digits (1, 3, 5, 7, 9), discard every even digit, and rearrange the remaining odd digits to form the smallest possible non-negative integer. The sign of n is irrelevant (treat the input by its absolute value). If n has no odd digits, return -1.
Examples:
12345-> odd digits are 1, 3, 5 -> smallest arrangement135.2468-> no odd digits ->-1.975312468-> odd digits 9,7,5,3,1 ->13579.
Return the resulting integer (or -1 when no odd digits exist).
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.
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
You are given a dictionary mapping each store name to a list of its comments. Find the comment(s) that appear in the most stores. Within a single store, identical comments are counted only once (deduplicate per store before counting), so a comment can contribute at most 1 to its cross-store count even if a store repeats it.
Return a list of all comments tied for the maximum cross-store count, sorted in ascending (lexicographic) order. If the input is empty, return an empty list.
Example: {'A': ['good','good','bad'], 'B': ['good','ok'], 'C': ['good','bad']} -> 'good' appears in all 3 stores, 'bad' in 2 stores, 'ok' in 1 -> return ['good'].
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.
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
You are given a list of classes. Each class is represented as [name, number_of_classes, start_year, end_year], meaning that class runs every calendar year from start_year through end_year (inclusive), contributing number_of_classes to each of those years.
Compute the maximum total number_of_classes that occurs across any two consecutive calendar years (year y and year y+1 combined). A single year with no following active year is also a valid window (effectively pairing it with a year that contributes 0), so the answer is at least the busiest single year. Return 0 if there are no classes.
Example: [['Math', 3, 2020, 2021], ['Science', 2, 2021, 2022]] -> year totals are 2020:3, 2021:5, 2022:2. The best consecutive pair is 2020+2021 = 8.
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.
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.