ZipRecruiter Phone Screen Coding Question: Max Occurring Names in Matrix
Question Details
Problem Statement Implement a function String[] getRestaurentWithMaxVotes(String[][] res) that determines which restaurant names appear most frequently in a given 2D array of strings. **Requirem
Full Details
Problem Statement Implement a function String[] getRestaurentWithMaxVotes(String[][] res) that determines which restaurant names appear most frequently in a given 2D array of strings. Requirements 1. Validation: Verify that all rows in the input 2D array are of equal length. If the shape is irregular (jagged array), return an empty array. 2. Frequency Counting: Count the occurrences of every restaurant name across the entire matrix. 3. Handling Ties: Identify the maximum frequency found. Return an array containing all names that match this maximum frequency. Example Input: java { {"a", "b", "c"}, {"a", "c", "d"} } Output: {"a", "c"} Explanation: Both "a" and "c" appear 2 times, which is the maximum occurrence count. "b" and "d" appear only once. Follow-Up: Weighted Scoring Modify the logic to calculate a "score" rather than a raw frequency count. The score depends on the rank (index) of the restaurant within its row. * Weight Logic: For a row of length $L$, the element at index $i$ receives a weight of $L - i$. * Example: In the row {"a", "b", "c"} (Length 3): * "a" (Index 0): $3 - 0 = 3$ points. * "b" (Index 1): $3 - 1 = 2$ points. * "c" (Index 2): $3 - 2 = 1$ point.