FastPrepCompact Conversations by Unique Listings

Compact Conversations by Unique Listings

Sierra logoSierra● MediumNEW GRADPHONE SCREEN
Learn

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 eviction finishes, 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 problems

See Sierra hiring insights
public String[] compactUniqueListings(String[][] conversations, int maxUniqueListings) {
    // Write your code here
}
conversations[["a","b","c"],["b","c","d"],["e"]]
maxUniqueListings4
expected["b", "c", "d", "e"]
Checking account…