Skip to the code
cf.
+10 XP

#694 Count Cache Misses

easy

Before choosing a cache size for a font loader, the team replays a log of font requests through a least recently used cache. A request is a hit if the font is already cached; otherwise it is a miss, the font is loaded into the cache, and if the cache now holds more than capacity fonts, the least recently used font is removed. Every request, hit or miss, counts as a use.

Write countMisses(capacity, requests) that returns the number of misses. The cache starts empty.

Example 1

Input: capacity = 2, requests = ["inter","roboto","inter","mono","roboto","inter"]
Output: 5

inter and roboto miss, inter hits, mono misses and evicts roboto, so roboto misses again and evicts inter, which then misses.

Example 2

Input: capacity = 3, requests = ["a","b","a","b","a"]
Output: 2

Only the first two requests miss.

Constraints

  • 1 <= capacity <= 1000
  • 0 <= requests.length <= 10^5
  • Requests are short strings.
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).