Sort Departments by Enrollment
Updated
Problem statement
You are given parallel arrays departmentIds and departmentNames. Index i describes one department whose unique identifier is departmentIds[i] and whose unique name is departmentNames[i]. You are also given studentDepartmentIds, where each value is the department identifier referenced by one student.
Return one row [departmentId, studentCount] for every department, including departments with no students. Sort the rows by studentCount in descending order. When two counts are equal, compare their unique names in ascending, case-sensitive ASCII code-point order.
Function
sortDepartmentsByEnrollment(departmentIds: int[], departmentNames: String[], studentDepartmentIds: int[]) → int[][]Examples
Example 1
departmentIds = [10,20,30]departmentNames = ["Sales","Engineering","Design"]studentDepartmentIds = [20,20,10]return = [[20,2],[10,1],[30,0]]Engineering has two students, Sales has one, and Design has none.
Example 2
departmentIds = [3,1,2]departmentNames = ["Zoo","Alpha","Beta"]studentDepartmentIds = [3,2]return = [[2,1],[3,1],[1,0]]Beta and Zoo each have one student, so their tie is resolved alphabetically. Alpha has no students and appears last.
Example 3
departmentIds = [8,4]departmentNames = ["Physics","Art"]studentDepartmentIds = []return = [[4,0],[8,0]]Both counts are zero, so Art precedes Physics alphabetically.
Constraints
1 <= departmentIds.length == departmentNames.length <= 100000.0 <= studentDepartmentIds.length <= 200000.- Department identifiers are unique signed 32-bit integers.
- Department names are unique non-empty strings containing visible ASCII characters.
- Every value in
studentDepartmentIdsoccurs indepartmentIds. - Lexicographic comparison is case-sensitive.
More Oracle Phone Screen questions
- Remove Parentheses from a Plus-Minus ExpressionPHONE SCREEN · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · Seen Oct 2026
- Shortest Window by Distinct-Value SumPHONE SCREEN · Seen Oct 2026
- Detect a Cycle in a Directed GraphPHONE SCREEN · Seen Oct 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Remove Adjacent Duplicates in String IIPHONE SCREEN · Seen Oct 2026
- Sum Root-to-Leaf Binary NumbersPHONE SCREEN · Seen Oct 2026
- Longest All-Present Attendance StreakPHONE SCREEN · Seen Oct 2026
- Next PermutationPHONE SCREEN · Seen Sep 2026
- Valid ParenthesesPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Longest Consecutive SequenceJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Top K Frequent Elements with a Recency TiebreakSwiggy · PHONE SCREEN
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Build a Task and Subtask HierarchyStripe · PHONE SCREENStripe interview questions
- Find All Points Within a RadiusGoogle · PHONE SCREENGoogle interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions