Question 1
Process Scheduler
Process scheduling algorithms are used by a CPU to optimally schedule the running processes. A core can execute one process at a time, but a CPU may have multiple cores.
There are n processes where the ith process starts its execution at start/i] and ends at end[il, both inclusive. Find the minimum number of cores required to execute the processes.
Example
n=3
start = [1, 3, 4]
end = [3, 5, 6].
If the CPU has only one core, the first process starts at 1 and ends at 3. The second process starts at 3. Since both processes need a processor at 3, they overlap. There must be more than 1 core.
If the CPU has two cores, the first process runs on the first core from 1 to 3, the second runs on the second core from 3 to 5, and the third process runs on the first core from 4 to 6.
Return 2, the minimum number of cores required.
Function Description
Complete the function getMinCores in the editor below.
getMinCores takes the following arguments: int start[n]: the start times of processes int end[n]: the end times of processes
Returns
int: the minimum number of cores required
Constraints
• 1 ≤n≤ 105
• 1 ≤ start[i] send[i] ≤ 109
STDIN FUNCTION
3 → start[] size n = 3
1 → start = [1, 2, 3]
2
3
3 → end[] size n = 3
3 → end = [3, 3, 5]
3
5
Sample Output
3
Explanation
Using 2 cores, the first and second processes finish at time
Question 2
Bitwise XOR Subsequences
A subsequence of an array is formed by removing zero or more elements from an array without changing the order of the remaining elements. In a valid subsequence, each pair of adjacent elements of the subsequence has bitwise XOR equal to k. Note that any subsequence of length 1 is valid regardless of the value of k, because there is no pair of adjacent elements in such a subsequence.
Given an array of integers and integer k, determine the length of the longest valid subsequence in the array.
Example n =5
arr = [2, 1, 3, 5, 2]
k=2
The subsequence [1,3] is valid because for all pairs of adjacent elements it has (1 and 3), the bitwise XOR of such
elements is equal to k (1 XOR 3 = 2). There are no other valid
subsequences that are longer than 2. For example, subsequence [2,1,3] is not valid because 2 XOR 1 does not equal 2.
Function Description
Complete the function maxSubsequenceLength in the editor below. The function must return the length of the longest valid subsequence of the array, given parameter k.
maxSubsequenceLength has the following parameter(S):
int n: the size of arr int arrin]: an array of integers int k: an integer
Constraints
• 1 ≤n≤ 105
• 0 ≤ arr[i] ≤ 106
• 0≤k≤ 106
Input Formatting
In the first line, there is a single integer, n, the number of elements in arr.
Each of the following n lines contains a single integer, arr[i]
In the last line, there is a single integer, k, the value the bitwise XOR of adjacent elements must equal for the subsequence to be valid.