Solve the following coding problems.
1) Bus schedule (next departure)
Given:
-
A sorted list
buses[]
of bus departure times (minutes since 00:00).
-
A list
arrivals[]
of passenger arrival times (minutes since 00:00).
For each passenger, return the earliest bus departure time that is >= their arrival time, or -1 if no such bus exists.
Constraints:
-
1 <= len(buses), len(arrivals) <= 2e5
-
Times fit in 32-bit signed integers.
2) Priority boarding order
You are given a list of passengers. Each passenger has:
-
priority
(smaller number = higher priority, e.g. 0 is best)
-
checkInTime
(integer)
-
name
(string)
Output the boarding order under these rules:
-
Higher priority boards first.
-
Within the same priority, earlier
checkInTime
boards first.
-
If still tied, preserve input order.
Follow-up: If passengers arrive as a stream, how would you produce the next passenger to board efficiently?
3) Decode passwords from a stream with limited reads
A file/stream contains multiple passwords encoded as (index, char) pairs in order of appearance. A password is defined by indices 0..L-1 appearing exactly once each, where L is the password length.
Rules:
-
The
first password is guaranteed valid
.
-
When parsing the stream, encountering an
index
that has already appeared in the
current
password indicates the start of the
next
password (i.e., the pair you just read belongs to the next password).
-
You cannot load the full file into memory. You may only read in chunks whose size is at most the
length
L of the first decoded password
.
Return all decoded passwords in order.
4) Evaluate an arithmetic expression with up to two variables
Given:
-
A string expression
expr
containing integers,
+
and
-
, parentheses
(
)
, optional whitespace, and variable names consisting of letters.
-
The expression contains
at most two distinct variable names
.
-
A map of variable values (e.g.,
{x: 3, y: 10}
), guaranteed to include any variables present.
Compute and return the integer result.
Note: You may not assume there are spaces between tokens.