Skip to the code
cf.
+10 XP

#285 Overwriting Breadcrumb Buffer

easy

An error reporter keeps the most recent user actions ("breadcrumbs") to attach to crash reports. It keeps at most capacity of them: when full, a new breadcrumb overwrites the oldest one. Write a class Breadcrumbs:

  • new Breadcrumbs(capacity) starts empty.
  • add(action) records an action string.
  • list() returns the stored actions from oldest to newest.
  • dropped() returns how many actions have been overwritten so far.

Use a fixed-size array with wrap-around; add must be O(1).

Each example lists the calls in order, then each call's arguments. The first call creates the object with new Breadcrumbs(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["Breadcrumbs","add","add","add","list","add","list","dropped"]
[[3],["click"],["scroll"],["type"],[],["submit"],[],[]]
Output: [null,null,null,null,["click","scroll","type"],null,["scroll","type","submit"],1]

submit overwrites the oldest action, click, so one action has been dropped.

Example 2

Input: ["Breadcrumbs","list","dropped"]
[[2],[],[]]
Output: [null,[],0]

Nothing has been recorded yet.

Constraints

  • 1 <= capacity <= 100
  • Actions are short strings.
  • At most 1000 calls.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).