FastPrepDetect Duplicate Trace IDs in a Linked List
Problems/Detect Duplicate Trace IDs in a Linked List

Detect Duplicate Trace IDs in a Linked List

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for a yes-or-no answer on duplicates in a linked list, a hash-set baseline, and a follow-up with O(1) space phrased as finding the repeated traceId. It does not say which value to return when several IDs repeat, how the list is written, whether you may change it, the string limits, or a time target. We kept the clear yes-or-no result. Our version is about 90% the same as the reported question.

You are given a finite acyclic singly linked list through three parallel inputs:

  • traceIds[i] is the case-sensitive trace ID stored at node i.
  • next[i] is the index of node i's successor, or -1 when i is the tail.
  • head is the first node index, or -1 for 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) → boolean

Examples

Example 1

traceIds = ["ingest","index","ingest"]next = [1,2,-1]head = 0return = true

Nodes 0 and 2 both store ingest.

Example 2

traceIds = ["root","api","db"]next = [2,-1,1]head = 0return = false

The traversal order is root -> db -> api, and every trace ID is distinct.

Example 3

traceIds = ["Trace-1","trace-1"]next = [1,-1]head = 0return = false

Trace 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 -1 or a valid node index.
  • Following next from head visits every represented node exactly once and ends at -1.
  • Each trace ID contains between 1 and 32 case-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

Interview reports that mention this question

  • Google Intern Prefix Sums and Heaps: 6 Experiences (Part 3 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public boolean hasDuplicateTraceId(String[] traceIds, int[] next, int head) {
      // Write your code here.
    }
}
Checking account…