Paginate Retained Conversation History
Updated
Problem statement
Interview-style brief
A conversation-history API returns messages one page at a time. Each page holds some messages and the cursor of the next page. Starting from the first cursor, keep following the next cursor until no pages are left, and keep only the most recent part of the history.
Rules
Each element of pages describes one page. pages[i][0] is the page's own cursor, pages[i][1] is the cursor of the next page, and the remaining entries are the page's messages from oldest to newest. An empty next cursor "" marks the last page. The pages are listed in no particular order, so follow the cursors rather than the array order.
Start with the page whose cursor is startCursor, read its messages, then move to the page named by its next cursor, and continue until you have read the last page. Messages on later pages are newer than messages on earlier pages.
While reading, keep at most historyLimit messages: whenever the history grows past the limit, drop the oldest message. Return the kept messages from oldest to newest. Each kept message appears exactly once, and messages with the same text are still separate messages.
Function
paginateConversationHistory(pages: String[][], startCursor: String, historyLimit: int) → String[]Examples
Example 1
pages = [["c2","c3","m3","m4"],["c1","c2","m1","m2"],["c3","","m5"]]startCursor = "c1"historyLimit = 4return = ["m2","m3","m4","m5"]Following the cursors from c1 reads the pages in the order c1, c2, c3, even though they are listed differently. That gives m1 through m5. Only 4 messages may be kept, so the oldest one, m1, is dropped.
Example 2
pages = [["start","","hi","How can I help?"]]startCursor = "start"historyLimit = 10return = ["hi","How can I help?"]There is one page, and its next cursor is empty, so it is also the last page. Both messages fit under the limit of 10.
Example 3
pages = [["p","q","a"],["r","","c","d"],["q","r","b"]]startCursor = "p"historyLimit = 1return = ["d"]The pages are read as p, q, r, giving a, b, c, d. With a limit of 1, only the newest message, d, is kept.
Constraints
1 <= pages.length <= 5003 <= pages[i].length <= 42, so every page has its cursor, its next cursor and1to40messages.- Cursors contain
1to20ASCII letters or digits, and the page cursors are distinct. Each next cursor is""or the cursor of another page. startCursoris a page cursor. Following next cursors from it visits every page exactly once and ends at the only page whose next cursor is"".- Every message has
1to50printable ASCII characters. 1 <= historyLimit <= 2000
More Sierra interview questions
Similar questions at other companies
- Lazy Filter over a GeneratorSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Array-Backed Set Without Library CollectionsOde with Anthropic · PHONE SCREEN
- Validate a Chess Bishop, Rook, or Queen MoveGlean · PHONE SCREENGlean interview questions
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Bounded IPv4 FilterAmazon · PHONE SCREEN
- Partition and Group Values by ParityEPAM · PHONE SCREENEPAM interview questions