Compact Conversations by Unique Listings
Updated
Problem statement
You are given an ordered array conversations. Each conversation contains listing identifiers, and an identifier may appear in several conversations or more than once in one conversation.
While the retained history contains more than maxUniqueListings distinct identifiers, remove the oldest entire conversation. Use identifier occurrence counts so an identifier stops contributing to the distinct total only after its final retained occurrence is removed.
After removing enough conversations, return the distinct identifiers from the retained conversations in first-appearance order. Include each identifier exactly once.
Function
compactUniqueListings(conversations: String[][], maxUniqueListings: int) → String[]Examples
Example 1
conversations = [["a","b","c"],["b","c","d"],["e"]]maxUniqueListings = 4return = ["b","c","d","e"]All conversations contain five distinct identifiers. Removing the oldest conversation deletes the final occurrence of "a", leaving the first-appearance order ["b","c","d","e"].
Example 2
conversations = [["x","x"],["x"]]maxUniqueListings = 1return = ["x"]The history already contains one distinct identifier, so no conversation is removed. Repeated occurrences produce one output entry.
Example 3
conversations = [["a"],["b"],["a"]]maxUniqueListings = 1return = ["a"]Removing the first conversation does not remove "a" from the distinct set because it appears later. Removing the second conversation deletes "b", leaving only "a".
Constraints
0 <= conversations.length <= 10^5- The total number of listing occurrences across all conversations is at most
10^5. - Every listing identifier is a non-empty ASCII string.
0 <= maxUniqueListings <= 10^5
More Sierra interview questions
Similar questions at other companies
- Shortest Window by Distinct-Value SumOracle · PHONE SCREENOracle interview questions
- Find Word-Multiset WindowsAmazon · PHONE SCREEN
- Transcript Window MetricsSesame · PHONE SCREEN
- Invalid Transactions in Time OrderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Count Equal-Offset Pairs Within an Index DistanceGoogle · PHONE SCREENGoogle interview questions
- Detect Security Attacks from Network LogsFireworks AI · PHONE SCREEN
- Smallest Range Covering Elements from K ListsSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions