In a coding interview, you are asked to solve two algorithm problems.
-
String reduction with adjacent duplicates
-
Basic version: Given a string
s
, repeatedly remove any adjacent pair of equal characters until no such pair remains. Return the final string.
-
Follow-up: Given a string
s
and an integer
k
, repeatedly remove any group of
k
adjacent equal characters until no more removals are possible. Return the final string.
-
Example follow-up: if
s = "deeedbbcccbdaa"
and
k = 3
, the answer is
"aa"
.
-
Time-based key-value store
-
Design a data structure that supports:
-
set(key, value, timestamp)
: store a value for a key at a given timestamp.
-
get(key, timestamp)
: return the value associated with the largest timestamp less than or equal to the given timestamp for that key. If no such timestamp exists, return an empty string.
-
Assume timestamps passed to
set
are strictly increasing for each key.
-
Implement both operations efficiently.
Write working code for both problems and explain the time and space complexity of your approach.