Detect Duplicate Trace IDs in a Linked List
Updated
Problem statement
You are given a finite acyclic singly linked list through three parallel inputs:
traceIds[i]is the case-sensitive trace ID stored at nodei.next[i]is the index of nodei's successor, or-1wheniis the tail.headis the first node index, or-1for an empty list.
Return true if two different reachable nodes have exactly the same trace ID. Otherwise, return false.
Do not modify traceIds or next. Your final solution must use O(1) auxiliary space; O(n^2) time is acceptable.
Function
hasDuplicateTraceId(traceIds: String[], next: int[], head: int) → booleanExamples
Example 1
traceIds = ["ingest","index","ingest"]next = [1,2,-1]head = 0return = trueNodes 0 and 2 both store ingest.
Example 2
traceIds = ["root","api","db"]next = [2,-1,1]head = 0return = falseThe traversal order is root -> db -> api, and every trace ID is distinct.
Example 3
traceIds = ["Trace-1","trace-1"]next = [1,-1]head = 0return = falseTrace ID comparison is case-sensitive, so the two values are different.
Constraints
0 <= traceIds.length = next.length <= 2000.- When the arrays are empty,
head == -1. Otherwise,0 <= head < traceIds.length. - Every
next[i]is-1or a valid node index. - Following
nextfromheadvisits every represented node exactly once and ends at-1. - Each trace ID contains between
1and32case-sensitive printable ASCII characters. - Do not use a map, set, or another auxiliary collection whose size grows with the list.
More Google Onsite Interview questions
- 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
- Route Pattern MatcherPHONE SCREEN · ONSITE 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
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Uncommon SubstringAirbnb · ONSITE INTERVIEWAirbnb interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Longest Substring Without Repeating CharactersOracle · ONSITE INTERVIEWOracle interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Classify Password StrengthHackerRank · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions