Direct Manager and Peer Queries
Updated
Problem statement
There are personCount people numbered from 0 through personCount - 1. Process the relationship operations from first to last.
[0, a, b]records that personais the direct manager of personb.[1, a, b]records that personsaandbare peers, meaning they have the same direct manager.[2, a, b]asks whether the facts recorded so far prove that personais the direct manager of personb.
All update operations are mutually consistent. A query whose manager is not yet known returns false.
Return the answers to all type-2 operations in their original order.
Function
answerManagerQueries(personCount: int, operations: int[][]) → boolean[]Examples
Example 1
personCount = 5operations = [[0,0,1],[1,1,2],[2,0,2],[2,3,2]]return = [true,false]Person 0 manages person 1. Because 1 and 2 are peers, person 0 also manages person 2. Person 3 is not their manager.
Example 2
personCount = 3operations = [[2,0,1],[0,0,1],[2,0,1],[1,1,2],[2,0,2]]return = [false,true,true]The first query is false because person 1's manager is still unknown. The manager assertion answers the second query, and the later peer assertion propagates that manager to person 2.
Example 3
personCount = 5operations = [[1,1,2],[0,4,2],[2,4,1],[0,0,3],[2,4,3]]return = [true,false]Peers 1 and 2 share manager 4. Person 3 instead has manager 0, so the final query is false.
Constraints
1 <= personCount <= 100000.1 <= operations.length <= 100000.- Every operation has exactly three integers
[type, a, b]. 0 <= type <= 2and0 <= a, b < personCount.- Type-
0and type-1operations never contradict facts recorded earlier. - At least one operation is a query.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Number of IslandsFleetx · ONSITE INTERVIEW
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
- Radar Barrier CrossingSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
- Number of IslandsWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Number of IslandsExpedia · ONSITE INTERVIEWExpedia interview questions
- Number of Islands IIAmazon · ONSITE INTERVIEW
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Group Linked Merchant RecordsStripe · PHONE SCREENStripe interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions