Implement short algorithms on logs, grids, and strings
Problem Description
This interview consists of several independent short coding tasks. For each task, you are required to implement the requested function(s). Standard library data structures are permitted.
1) Detect CPU Temperature Spikes
You will receive a list of CPU temperature readings, (timestamp, tempC), which is sorted by timestamp in ascending order. A spike at reading i (with time t_i and temperature x_i) is defined if there exists some earlier reading j < i such that:
t_i - t_j <= windowSeconds(the time difference is within the specified window), ANDx_i - x_j >= deltaC(the temperature increase meets or exceeds the specified delta).
Input:
readings: List[Tuple[int timestamp, int tempC]](timestamps are in seconds)windowSeconds: intdeltaC: int
Output:
- Return a list of timestamps
t_icorresponding to all readingsithat are identified as spikes.
Constraints: The number of readings can be up to 2e5.
2) Minimum Possible Sum After K Operations
Given an array a of non-negative integers and an integer k, you must perform the following operation exactly k times:
- Choose an index
iand replacea[i]withceil(a[i] / 2).
Goal: Your objective is to minimize the final sum of all elements in the array after k operations.
Input:
a: List[int]k: int
Output:
- Return the minimum achievable sum after
koperations.
Constraints: The array size n can be up to 2e5, values in a can be up to 1e9, and k can be up to 2e5.
3) Verify Matching Brackets in a Mathematical Expression
Given a string s that represents a mathematical expression, which may contain characters including ()[]{} and other non-bracket characters, determine whether all brackets are correctly matched and properly nested.
Input:
s: str
Output:
- Return
Trueif the brackets are valid, otherwiseFalse.
Notes: Non-bracket characters within the string s should be ignored during validation.
4) Aggregate HTTP Logs by Status Code
You will be provided with a list of log lines. Each line follows the format: "<statusCode> <responseTimeMs>".
Example: "200 153", "500 12".
Task: Aggregate the log data by statusCode to calculate the following for each status code:
- The total count of requests.
- The average response time (as a floating-point value).
Input:
logs: List[str]
Output:
- Return a mapping (e.g., dictionary or hash map) where keys are
statusCodeand values are tuples(count, avgResponseTime).
Edge Cases: Consider how to handle an empty input list. For malformed log lines, you should state and implement a consistent rule for ignoring them (e.g., skip the line, or raise an error if strict parsing is required).
5) Tree Planting on a Grid (No Adjacent Trees)
You are given an m x n grid named land where:
1indicates an existing tree.0indicates empty land.
Your task is to plant additional trees on empty cells (0), with the constraint that no two trees (whether existing or newly planted) may be adjacent in the 4-neighborhood (up, down, left, or right).
Task: Determine a set of cells to plant new trees such that the number of newly planted trees is maximum in size.
Input:
land: List[List[int]]
Output:
- Return
maxNewTrees: int, which is the maximum number of new trees that can be planted. - Optionally, if specifically requested, return one valid resulting grid configuration that achieves this maximum.
Constraints: The grid dimensions m and n can be up to approximately 50. State any assumptions made and optimize your solution accordingly.
AI interview prep
Core dilemma
The central engineering conflict in this problem set is demonstrating broad algorithmic proficiency, adaptability across diverse problem domains (data streams, numerical optimization, string parsing, grid-based combinatorial problems), and the ability to implement optimal solutions under time pressure, while maintaining code quality and correctness for each distinct challenge.
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 short algorithms on logs, grids, and strings
Problem Description
This interview consists of several independent short coding tasks. For each task, you are required to implement the requested function(s). Standard library data structures are permitted.
1) Detect CPU Temperature Spikes
You will receive a list of CPU temperature readings, (timestamp, tempC), which is sorted by timestamp in ascending order. A spike at reading i (with time t_i and temperature x_i) is defined if there exists some earlier reading j < i such that:
t_i - t_j <= windowSeconds(the time difference is within the specified window), ANDx_i - x_j >= deltaC(the temperature increase meets or exceeds the specified delta).
Input:
readings: List[Tuple[int timestamp, int tempC]](timestamps are in seconds)windowSeconds: intdeltaC: int
Output:
- Return a list of timestamps
t_icorresponding to all readingsithat are identified as spikes.
Constraints: The number of readings can be up to 2e5.
2) Minimum Possible Sum After K Operations
Given an array a of non-negative integers and an integer k, you must perform the following operation exactly k times:
- Choose an index
iand replacea[i]withceil(a[i] / 2).
Goal: Your objective is to minimize the final sum of all elements in the array after k operations.
Input:
a: List[int]k: int
Output:
- Return the minimum achievable sum after
koperations.
Constraints: The array size n can be up to 2e5, values in a can be up to 1e9, and k can be up to 2e5.
3) Verify Matching Brackets in a Mathematical Expression
Given a string s that represents a mathematical expression, which may contain characters including ()[]{} and other non-bracket characters, determine whether all brackets are correctly matched and properly nested.
Input:
s: str
Output:
- Return
Trueif the brackets are valid, otherwiseFalse.
Notes: Non-bracket characters within the string s should be ignored during validation.
4) Aggregate HTTP Logs by Status Code
You will be provided with a list of log lines. Each line follows the format: "<statusCode> <responseTimeMs>".
Example: "200 153", "500 12".
Task: Aggregate the log data by statusCode to calculate the following for each status code:
- The total count of requests.
- The average response time (as a floating-point value).
Input:
logs: List[str]
Output:
- Return a mapping (e.g., dictionary or hash map) where keys are
statusCodeand values are tuples(count, avgResponseTime).
Edge Cases: Consider how to handle an empty input list. For malformed log lines, you should state and implement a consistent rule for ignoring them (e.g., skip the line, or raise an error if strict parsing is required).
5) Tree Planting on a Grid (No Adjacent Trees)
You are given an m x n grid named land where:
1indicates an existing tree.0indicates empty land.
Your task is to plant additional trees on empty cells (0), with the constraint that no two trees (whether existing or newly planted) may be adjacent in the 4-neighborhood (up, down, left, or right).
Task: Determine a set of cells to plant new trees such that the number of newly planted trees is maximum in size.
Input:
land: List[List[int]]
Output:
- Return
maxNewTrees: int, which is the maximum number of new trees that can be planted. - Optionally, if specifically requested, return one valid resulting grid configuration that achieves this maximum.
Constraints: The grid dimensions m and n can be up to approximately 50. State any assumptions made and optimize your solution accordingly.