This multi-part question evaluates algorithmic problem-solving and practical implementation skills, covering grid geometry and distance metrics for nearest-item queries, expression parsing and nested evaluation for a command-based calculator, and time-indexed data structure design for versioned key-value storage in the Coding & Algorithms domain.
You are given three independent interview-style coding tasks.
You are given an m x n grid of integers:
1
= cake
0
= person
Assume at least one cake and at least one person exist.
Compute the minimum Manhattan distance between any person cell and any cake cell.
(r1,c1)
and
(r2,c2)
is
|r1-r2| + |c1-c2|
.
Each person will eat the closest cake (by Manhattan distance). You are given the position (or index) of one specific person, and you must return which cake that person will eat.
Additional guarantees:
These guarantees imply the assignment is deterministic (no ambiguous choices).
(rp, cp)
(known to be a
0
).
(rc, cc)
that this person eats.
You are given a list of strings commands, each command being one of:
"ADD x"
"SUB x"
"MULT x"
"DIV x"
where x is an integer (may be negative).
Start with an accumulator value acc = 0. Process commands in order, updating acc each time. DIV should use truncation toward zero (like integer division in many languages).
acc
after all commands.
Extend the calculator to support nested expressions inside a command, e.g. operands can themselves be expressions, such as:
"ADD (MULT 3 (ADD 2 5))"
Define a clear grammar and evaluate the expression correctly.
Implement an in-memory store supporting:
set(key, value, timestamp)
get(key, timestamp)
→ returns the value associated with
key
at the greatest timestamp
t'
such that
t' <= timestamp
. If none exists, return an empty value.
Assume:
key
and
value
are strings
timestamp
is an integer
You should design data structures and implement these operations efficiently.