You are given several independent short coding tasks. For each task, implement the requested function(s). Assume standard library data structures are allowed.
1) Detect CPU temperature spikes
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:
-
Return the list of timestamps
t_i
where reading
i
is a spike.
Constraints (example): up to 2e5 readings.
2) Minimum possible sum after K operations
Given an array of non-negative integers a and an integer k, you may perform the following operation exactly k times:
-
Choose an index
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.
3) Verify matching brackets in a mathematical expression
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.
4) Aggregate HTTP logs by status code
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:
-
count of requests
-
average response time (as a floating-point value)
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).
5) Tree planting on a grid (no adjacent trees)
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
-
optionally (if asked), one valid resulting grid configuration
Constraints (example): m, n up to ~50 (state assumptions and optimize accordingly).