Deduplicate Logs: Keep First
Updated
Problem statement
You are given log entries in encounter order. The ith entry is the pair (timestamps[i], messages[i]). Two entries are duplicates when their messages are equal.
For each distinct message, keep only its first encountered entry. This means the earliest array position wins, even if a later duplicate has a smaller timestamp.
Sort the retained pairs by timestamp in ascending order. When two retained pairs have the same timestamp, sort them by message in ascending lexicographic order.
Return the sorted pairs as a String[]. Encode each pair as timestamp|message, using the timestamp's ordinary decimal representation.
Function
deduplicateLogsKeepFirst(timestamps: int[], messages: String[]) → String[]Examples
Example 1
timestamps = [30,10,20,15,20]messages = ["cache miss","user login","cache miss","heartbeat","user login"]return = ["10|user login","15|heartbeat","30|cache miss"]The first encountered entries for cache miss, user login, and heartbeat have timestamps 30, 10, and 15. The later duplicates are discarded, even though the later cache miss has a smaller timestamp. Sorting the three retained pairs gives the result.
Example 2
timestamps = [5,5,3,4]messages = ["beta","alpha","beta","gamma"]return = ["4|gamma","5|alpha","5|beta"]The later beta entry is removed. The retained alpha and beta entries both have timestamp 5, so their messages determine their order.
Example 3
timestamps = []messages = []return = []There are no log entries, so there are no retained pairs.
Constraints
timestamps.length == messages.length0 <= timestamps.length <= 200000-10^9 <= timestamps[i] <= 10^91 <= messages[i].length <= 100- Each message contains printable ASCII characters other than
|. - The total length of all messages does not exceed
10^6.
More Google Onsite Interview questions
- 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
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Count Word FrequenciesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Most Voted CandidateExpedia · ONSITE INTERVIEWExpedia interview questions
- List Each Name Once, AlphabeticallyDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Group Duplicate Files by ContentPostman · ONSITE INTERVIEWPostman interview questions
- Group AnagramsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
Google interview experiences
- Google L4 SWE Phone Screen: 1 Experience
- Google ML Engineering Manager Onsite: 1 Experience
- Google Experienced SWE First Round and Onsite: 1 Experience
- Google AI Coding Interview and AI OA: 2 Experiences
- Google SWE Intern Interview Results Timeline: 13 Experiences
- Google L4 SWE Phone Screen and Kirkland Onsite: 1 Experience