You are given several independent coding tasks that appeared across an OA and frontend-focused interviews. Implement each as specified.
1) Time-based key-value store
Design a data structure that supports storing multiple values for the same key at different timestamps.
Operations
-
set(key: string, value: string, timestamp: int) -> void
-
get(key: string, timestamp: int) -> string
get rule
Return the value associated with key at the largest timestamp t such that t <= timestamp. If no such timestamp exists for that key, return an empty string.
Constraints (typical)
-
Many calls; aim for ~
O(log n)
per
get
per key.
-
Timestamps for a given key are non-decreasing across
set
calls.
2) Remove duplicates from a linked list
Given the head of a sorted singly linked list, remove duplicates so that each value appears only once.
-
Input:
head
of a sorted list.
-
Output: the modified list head.
Example: 1 -> 1 -> 2 -> 3 -> 3 becomes 1 -> 2 -> 3.
3) Implement a curried “add” function in JavaScript
Implement a JavaScript function add that supports chained calls via currying.
Required behavior (pick one clear contract and implement it)
Option A (explicit terminator):
-
add(1)(2)(3)()
returns
6
.
Option B (implicit coercion):
-
add(1)(2)(3) == 6
is
true
(via
valueOf
/
toString
).
Handle common edge cases (e.g., add(0), negative numbers).
4) Traffic light scheduler (frontend async)
Implement a function that repeatedly cycles through traffic lights in order:
-
Red for 3 seconds
-
Green for 2 seconds
-
Yellow for 1 second
Requirements:
-
The sequence must repeat indefinitely (or for
n
cycles if you choose to add a parameter).
-
Use asynchronous control flow (Promises /
async
/
await
/ timers).
-
Each transition should trigger a callback/log such as
console.log('red')
at the moment the light turns on.