FastPrepPaginate Retained Conversation History

Paginate Retained Conversation History

Sierra logoSierra● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

You are given an ordered conversation history messages, a positive retention limit historyLimit, and a positive page size pageSize.

First compact the history by discarding messages from the beginning until at most the newest historyLimit messages remain. Then split that retained suffix into consecutive pages of at most pageSize messages.

Return the pages in chronological order. Every retained message must appear exactly once, and no discarded message may appear.

Function

paginateConversationHistory(messages: String[], historyLimit: int, pageSize: int) → String[][]

Examples

Example 1

messages = ["m1","m2","m3","m4","m5"]historyLimit = 4pageSize = 2return = [["m2","m3"],["m4","m5"]]

The newest four messages are ["m2","m3","m4","m5"]. Splitting that retained suffix into pages of size 2 yields the two returned pages.

Example 2

messages = ["a","b","c"]historyLimit = 10pageSize = 2return = [["a","b"],["c"]]

All three messages fit within historyLimit. The final page contains the one remaining message.

Example 3

messages = []historyLimit = 3pageSize = 2return = []

An empty history has no retained messages and therefore no pages.

Constraints

  • 0 <= messages.length <= 10^5
  • Every entry in messages is a non-empty ASCII string.
  • 1 <= historyLimit <= 10^5
  • 1 <= pageSize <= 10^5

More Sierra problems

See Sierra hiring insights
public String[][] paginateConversationHistory(String[] messages, int historyLimit, int pageSize) {
    // Write your code here
}
messages["m1","m2","m3","m4","m5"]
historyLimit4
pageSize2
expected[["m2", "m3"], ["m4", "m5"]]
Checking account…