Problem · Design

Chatter Message Window

Learn this problem
HardReddit logoRedditFULLTIMEPHONE SCREEN

Problem statement

Process an ordered sequence of operations on messages with unique decimal IDs. Messages are loaded in strictly increasing numeric ID order across all batches.

  • ["LOAD", id1, text1, id2, text2, ...] appends a non-empty ordered batch and produces no result.
  • ["SAVE"] returns all current messages in ID order as [id1, text1, id2, text2, ...].
  • ["GET_MESSAGES", id] returns the identified message, up to two preceding messages, and up to two following messages in the same flattened form. A missing ID returns an empty list.
  • ["GET_MULTI", id1, id2, ...] unions the windows for all existing requested IDs, removes duplicate messages, and returns them in numeric ID order.
  • ["EDIT", id, newText] replaces the current text and returns ["true"], or returns ["false"] for a missing ID.
  • ["GET_HISTORY", id] returns the initially loaded text followed by every successful edit text in chronological order, or an empty list for a missing ID.

Return the result of every non-LOAD operation in operation order. Repeated read queries do not change state.

Function

processChatter(operations: String[][]) → String[][]

Examples

Example 1

operations = [["LOAD","1","one","2","two","3","three","4","four","5","five"],["GET_MESSAGES","3"],["GET_MESSAGES","1"],["GET_MULTI","2","5"],["SAVE"]]return = [["1","one","2","two","3","three","4","four","5","five"],["1","one","2","two","3","three"],["1","one","2","two","3","three","4","four","5","five"],["1","one","2","two","3","three","4","four","5","five"]]

The window centered at 3 reaches both boundaries. The windows for 2 and 5 overlap, so their union contains each message once.

Example 2

operations = [["LOAD","1.10","a","2.25","b","4","c"],["LOAD","5","d","9","e"],["GET_MESSAGES","4"],["EDIT","4","C2"],["GET_MESSAGES","4"],["GET_HISTORY","4"],["SAVE"]]return = [["1.10","a","2.25","b","4","c","5","d","9","e"],["true"],["1.10","a","2.25","b","4","C2","5","d","9","e"],["c","C2"],["1.10","a","2.25","b","4","C2","5","d","9","e"]]

Editing changes the current message used by reads and save, while history retains both the loaded and edited text.

Example 3

operations = [["LOAD","10","a","20","b","30","c"],["GET_MESSAGES","99"],["EDIT","99","missing"],["GET_HISTORY","99"],["EDIT","20","B"],["EDIT","20","B2"],["GET_HISTORY","20"],["GET_MULTI","10","10","99"]]return = [[],["false"],[],["true"],["true"],["b","B","B2"],["10","a","20","B2","30","c"]]

Missing IDs have total deterministic results. Repeating 10 in GET_MULTI does not duplicate its three-message boundary window.

Constraints

  • 1 <= operations.length <= 10^4.
  • Across all loads there are at most 10^5 messages, and across all multi-queries there are at most 10^5 requested IDs.
  • Message IDs are canonical finite decimal strings with at most two fractional digits, fit after scaling by 100 in a signed 64-bit integer, are unique, and arrive in strictly increasing numeric order.
  • Message text contains printable ASCII characters and has length at most 200.
  • Every operation has a supported opcode and the exact arity described above.

More Reddit problems

drafts saved locally
public String[][] processChatter(String[][] operations) {
  // write your code here
}
operations[["LOAD","1","one","2","two","3","three","4","four","5","five"],["GET_MESSAGES","3"],["GET_MESSAGES","1"],["GET_MULTI","2","5"],["SAVE"]]
expected[["1", "one", "2", "two", "3", "three", "4", "four", "5", "five", "1", "one", "2", "two", "3", "three", "1", "one", "2", "two", "3", "three", "4", "four", "5", "five", "1", "one", "2", "two", "3", "three", "4", "four", "5", "five"]]
checking account