This multi-part Coding & Algorithms question evaluates algorithmic problem-solving skills and mastery of core competencies including temporal window reasoning, greedy optimization under repeated operations, bracket-matching/parsing, log aggregation and parsing, and combinatorial placement on grids.
You are given several independent short coding tasks. For each task, implement the requested function(s). Assume standard library data structures are allowed.
You receive CPU temperature readings as a list of pairs (timestamp, tempC) sorted by timestamp ascending.
Define a spike at reading i (time t_i, temperature x_i) if there exists some earlier reading j < i such that:
t_i - t_j <= windowSeconds
, and
x_i - x_j >= deltaC
.
Input:
readings: List[Tuple[int timestamp, int tempC]]
(timestamps are seconds)
windowSeconds: int
deltaC: int
Output:
t_i
where reading
i
is a spike.
Constraints (example): up to 2e5 readings.
Given an array of non-negative integers a and an integer k, you may perform the following operation exactly k times:
i
and replace
a[i]
with
ceil(a[i] / 2)
.
Goal: minimize the final sum of the array.
Input: a: List[int], k: int
Output: the minimum achievable sum after k operations.
Constraints (example): n up to 2e5, values up to 1e9, k up to 2e5.
Given a string s representing a mathematical expression containing characters including ()[]{} and other non-bracket characters, determine whether the brackets are correctly matched and nested.
Input: s: str
Output: True if valid, else False.
Notes: Non-bracket characters should be ignored.
You are given log lines. Each line contains a status code and a response time in milliseconds:
Format: "<statusCode> <responseTimeMs>"
Example: "200 153", "500 12".
Task: aggregate by statusCode:
Input: logs: List[str]
Output: a mapping/dictionary statusCode -> (count, avgResponseTime).
Edge cases: empty input; malformed lines may be ignored (state and implement a consistent rule).
You are given an m x n grid land with:
1
meaning an existing tree
0
meaning empty land
You may plant additional trees on empty cells, but no two trees (existing or newly planted) may be adjacent in the 4-neighborhood (up/down/left/right).
Task: determine a set of cells to plant new trees that is maximum in size.
Input: land: List[List[int]]
Output:
maxNewTrees: int
, the maximum number of new trees that can be planted
Constraints (example): m, n up to ~50 (state assumptions and optimize accordingly).