Group Names by Department
Updated
Problem statement
You are given a matrix people. Each row contains exactly two strings:
people[i][0]is a person's name.people[i][1]is that person's department.
Group the names by department.
Return a matrix in which each row begins with a department, followed by the names assigned to it. Order department rows by each department's first appearance in people, and preserve input order among names in the same department.
Function
groupNamesByDepartment(people: String[][]) → String[][]Examples
Example 1
people = [["Ava","Data"],["Ben","Risk"],["Chen","Data"],["Diya","Risk"]]return = [["Data","Ava","Chen"],["Risk","Ben","Diya"]]Data appears first, followed by Risk. Names remain in their original order within each department.
Example 2
people = [["Mina","Finance"],["Noah","Engineering"],["Omar","Finance"]]return = [["Finance","Mina","Omar"],["Engineering","Noah"]]The Finance row is first because that department is encountered first.
Example 3
people = [["Zoe","Legal"]]return = [["Legal","Zoe"]]A single record creates one department row.
Constraints
1 <= people.length <= 10^5.people[i].length = 2.- Every name and department is non-empty.
- The total number of characters in
peopleis at most10^6.
More Mastercard Phone Screen questions
- Top Three Accounts by Monthly Transaction AmountPHONE SCREEN · Seen Sep 2026
- Deduplicate a List While Preserving OrderPHONE SCREEN · Seen Jul 2026
- Employee With the Highest SalaryPHONE SCREEN · Seen Jul 2026
- Credit and Debit Transaction Counts per UserPHONE SCREEN · Seen Apr 2026
- Users with Three Transactions in Sixty SecondsPHONE SCREEN · Seen Apr 2026
- Container With Most WaterPHONE SCREEN · Seen Oct 2025
- Employees with the Highest and Lowest SalaryPHONE SCREEN · Seen Apr 2025
- Inactive Customers with Prior ActivityPHONE SCREEN · Seen Nov 2024
- Merchant Category Transaction SummaryPHONE SCREEN · Seen Nov 2024
- Top Customers by Six-Month Transaction AmountPHONE SCREEN · Seen Nov 2024
Similar questions at other companies
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Bounded IPv4 FilterAmazon · PHONE SCREEN
- SimpleRDD Multiset IntersectionDatologyAI · PHONE SCREEN
- Vehicle Maintenance Event ReadinessWaymo · PHONE SCREENWaymo interview questions
- Summarize the Latest Event for Each DeviceOneTrust · PHONE SCREEN
- Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
- Employee Training StatusVanta · PHONE SCREEN
- Select Object by Key ExtremeBridge · PHONE SCREENBridge interview questions
- Badge Records Without a MatchWhatNot · PHONE SCREENWhatNot interview questions
- AI Role: Collection with Add and Remove OperationsApple · PHONE SCREENApple interview questions