AI Native challenges by level

Level 3 · Advanced · Structure and efficiency

Repair an expiring cache

Fix expiration boundaries, updates, and recency in an existing cache.

Problem statement

Write function solution(capacity, operations). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. The starter mishandles expiration, replacement, recency, and deletion. Repair it into an expiring LRU cache; you may rewrite the function body. capacity is an integer from 0 through 1024. operations contains put {type:"put",key:string,value:integer,timestamp:nonnegative integer,ttlMs:nonnegative integer}, get {type:"get",key:string,timestamp}, or delete {type:"delete",key:string,timestamp}. Times are milliseconds and timestamps are nondecreasing. Keys are case-sensitive. Before each operation remove all entries with expiration<=timestamp. put replaces the value, sets expiration=timestamp+ttlMs, and makes it most recently used. ttlMs=0 removes the existing key without storing a value. capacity=0 stores nothing. Over capacity, evict the least recently used unexpired entry. get returns a valid value and updates recency without extending expiration; a miss returns null without changing recency. delete removes the key; missing keys are ignored. An old expiration must not delete a value replaced by a newer put. Process equal timestamps in input order. Return only get results in order. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.

Example input and output

Example 1

Function arguments

[
  2,
  [
    {
      "type": "put",
      "key": "a",
      "value": 1,
      "timestamp": 0,
      "ttlMs": 10
    },
    {
      "type": "get",
      "key": "a",
      "timestamp": 9
    },
    {
      "type": "get",
      "key": "a",
      "timestamp": 10
    }
  ]
]

Return value

[
  1,
  null
]
Example 2

Function arguments

[
  2,
  [
    {
      "type": "put",
      "key": "a",
      "value": 1,
      "timestamp": 0,
      "ttlMs": 5
    },
    {
      "type": "put",
      "key": "a",
      "value": 2,
      "timestamp": 2,
      "ttlMs": 10
    },
    {
      "type": "get",
      "key": "a",
      "timestamp": 5
    },
    {
      "type": "get",
      "key": "a",
      "timestamp": 12
    }
  ]
]

Return value

[
  2,
  null
]
Example 3

Function arguments

[
  1,
  [
    {
      "type": "put",
      "key": "a",
      "value": 1,
      "timestamp": 0,
      "ttlMs": 2
    },
    {
      "type": "delete",
      "key": "a",
      "timestamp": 1
    },
    {
      "type": "put",
      "key": "a",
      "value": 2,
      "timestamp": 1,
      "ttlMs": 10
    },
    {
      "type": "get",
      "key": "a",
      "timestamp": 2
    }
  ]
]

Return value

[
  2
]
Practice has no points, deadline or submission cap. Use AI and search freely. Sign in to run public examples, or verify locally.

541 / 20,000 characters

Add your test · optional

Enter a JSON argument array in capacity, operations order. Your test runs alongside the three examples.

Save both files in one folder and run this command with Node.js. Read the checks field for individual example results.

node verify-repair-ttl-cache.mjs
문의하기

서비스명: 포트포올
문의는 운영자에게 이메일로 전송됩니다.

이메일과 문의 내용은 답변을 위해 운영자 및 메일 발송 서비스로 전송됩니다. 비밀번호·결제정보·민감한 개인정보는 입력하지 마세요.