Solve three scheduling and array problems
Problem 1: Schedule Tasks with Cooldown
You are given a list of tasks, where each task is represented by a capital letter from 'A' to 'Z', and an integer n (where n >= 0) representing a cooldown period.
A single CPU can execute at most one task per time unit. The same type of task must be separated by at least n idle intervals (or other tasks) between two consecutive executions. You are allowed to insert idle slots (where no task is executed) if necessary.
Your goal is to return the minimum number of time units required to execute all tasks.
Example:
- Input:
tasks = ['A','A','A','B','B','B'],n = 2 - One optimal schedule:
A B idle A B idle A B - Output:
8
Constraints:
1 <= tasks.length <= 10^40 <= n <= 10^4
Describe an efficient algorithm and then implement it.
Problem 2: Remove Duplicates from Sorted Array In-Place
You are given a sorted integer array nums (in non-decreasing order).
Your task is to remove the duplicates in-place such that each unique element appears only once, while preserving the relative order of the elements. The function should return the new length k of the array containing only the unique elements. The first k elements of nums should hold the unique values; the content of the array beyond k is not important.
You must use O(1) extra space.
Example:
- Input:
nums = [0,0,1,1,1,2,2,3,3,4] - Output:
- Return value:
5 numsis modified to start with[0,1,2,3,4, ...]
- Return value:
Constraints:
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Describe your approach (e.g., pointer technique) and implement the function.
Problem 3: Rearrange String to Avoid Adjacent Duplicates
You are given a string s consisting of lowercase English letters.
Rearrange the characters of s so that no two adjacent characters are the same. If it is not possible to achieve such a rearrangement, you should indicate failure (e.g., by returning an empty string or a special value as defined by the language/boilerplate).
Example 1:
- Input:
s = "aab" - One valid output:
"aba"
Example 2:
- Input:
s = "aaab" - Output: impossible (because any arrangement will have at least two adjacent 'a's).
Constraints:
1 <= s.length <= 10^5scontains only lowercase English letters 'a' to 'z'.
Design an efficient algorithm (considering both time and space complexity) and implement it.
AI interview prep
Core dilemma
Effectively managing time and demonstrating robust algorithmic problem-solving skills across diverse problem types (scheduling, array manipulation, string rearrangement) within a single interview round, while prioritizing optimal solutions and clear communication.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve three scheduling and array problems
Problem 1: Schedule Tasks with Cooldown
You are given a list of tasks, where each task is represented by a capital letter from 'A' to 'Z', and an integer n (where n >= 0) representing a cooldown period.
A single CPU can execute at most one task per time unit. The same type of task must be separated by at least n idle intervals (or other tasks) between two consecutive executions. You are allowed to insert idle slots (where no task is executed) if necessary.
Your goal is to return the minimum number of time units required to execute all tasks.
Example:
- Input:
tasks = ['A','A','A','B','B','B'],n = 2 - One optimal schedule:
A B idle A B idle A B - Output:
8
Constraints:
1 <= tasks.length <= 10^40 <= n <= 10^4
Describe an efficient algorithm and then implement it.
Problem 2: Remove Duplicates from Sorted Array In-Place
You are given a sorted integer array nums (in non-decreasing order).
Your task is to remove the duplicates in-place such that each unique element appears only once, while preserving the relative order of the elements. The function should return the new length k of the array containing only the unique elements. The first k elements of nums should hold the unique values; the content of the array beyond k is not important.
You must use O(1) extra space.
Example:
- Input:
nums = [0,0,1,1,1,2,2,3,3,4] - Output:
- Return value:
5 numsis modified to start with[0,1,2,3,4, ...]
- Return value:
Constraints:
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Describe your approach (e.g., pointer technique) and implement the function.
Problem 3: Rearrange String to Avoid Adjacent Duplicates
You are given a string s consisting of lowercase English letters.
Rearrange the characters of s so that no two adjacent characters are the same. If it is not possible to achieve such a rearrangement, you should indicate failure (e.g., by returning an empty string or a special value as defined by the language/boilerplate).
Example 1:
- Input:
s = "aab" - One valid output:
"aba"
Example 2:
- Input:
s = "aaab" - Output: impossible (because any arrangement will have at least two adjacent 'a's).
Constraints:
1 <= s.length <= 10^5scontains only lowercase English letters 'a' to 'z'.
Design an efficient algorithm (considering both time and space complexity) and implement it.