Implement string reduction and time map
Problem 1: String Reduction
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 Version
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 for Follow-up:
If s = "deeedbbcccbdaa" and k = 3, the answer is "aa".
Problem 2: Time-Based Key-Value Store
Design a data structure that supports the following operations:
set(key, value, timestamp): Stores avaluefor a givenkeyat a specifictimestamp.get(key, timestamp): Returns thevalueassociated with the largesttimestampthat is less than or equal to the giventimestampfor thatkey. If no such timestamp exists, return an empty string.
Constraints & Assumptions:
- Timestamps passed to
setare strictly increasing for eachkey. - Both
setandgetoperations must be implemented efficiently.
Write working code for both problems and explain the time and space complexity of your approach for each.
AI interview prep
Core dilemma
For the Time-Based Key-Value Store, the central dilemma is balancing efficient retrieval of historical data (get operation, requiring a 'floor' search) with potentially high-volume, strictly increasing writes (set operation) for multiple keys, ensuring optimal time and space complexity under varying access patterns.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement string reduction and time map
Problem 1: String Reduction
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 Version
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 for Follow-up:
If s = "deeedbbcccbdaa" and k = 3, the answer is "aa".
Problem 2: Time-Based Key-Value Store
Design a data structure that supports the following operations:
set(key, value, timestamp): Stores avaluefor a givenkeyat a specifictimestamp.get(key, timestamp): Returns thevalueassociated with the largesttimestampthat is less than or equal to the giventimestampfor thatkey. If no such timestamp exists, return an empty string.
Constraints & Assumptions:
- Timestamps passed to
setare strictly increasing for eachkey. - Both
setandgetoperations must be implemented efficiently.
Write working code for both problems and explain the time and space complexity of your approach for each.