IBM Online Assessment -On Campus-2 Coding Problems
Summary
I completed the IBM online assessment, which consisted of two coding problems.
Full Experience
I recently completed an IBM Online Assessment and wanted to share the coding questions I encountered.
Platform:HackerRank Time:70 minutes
Question 1 – Make the Array Alternating in Parity
Given an array of positive integers, find the minimum number of operations required to make the array alternate in parity.
The final array can follow either of these patterns:
Odd, Even, Odd, Even, ...Even, Odd, Even, Odd, ...
Operation
In one operation, you can replace any element:
nums[i] = floor(nums[i] / 2)
You can perform the operation on the same element multiple times.
Example
nums = [6, 12, 5, 10]
For the pattern:
Even, Odd, Even, Odd
We can transform:
6 -> 6 (Even)
12 -> 6 -> 3 (Odd) -> 2 operations
5 -> 2 (Even) -> 1 operation
10 -> 10 (Even)
So the total is:
3 operations
For the pattern:
odd, even, odd, even
We can transform:
6 -> 3 (odd) -> 1 operations
12 -> (Even)
5 -> 2 (odd)
10 -> 10 (Even)
So the answer is:
1 operation
Another example:
nums = [3, 12, 5, 10]
The array is already:
Odd, Even, Odd, Even
so the answer is:
0
If considering a transformation such as:
6 -> 3
that costs one operation.
Constraints
1 <= nums.size() < 10^5
nums[i] > 0
Question 2 – Maximum Number of Valid Pairs
Given two arrays arr1 and arr2, and an integer d, form the maximum number of pairs (arr1[i], arr2[j]) satisfying:
arr1[i] <= arr2[j] <= arr1[i] + d
Each element can be used in at most one pair.
Example
arr1 = [8, 20, 35, 45]
arr2 = [25, 50]
d = 10
Possible valid pairs include:
(20, 25) because 20 <= 25 <= 30
(45, 50) because 45 <= 50 <= 55
Therefore:
Answer = 2
Constraints
1 <= arr1.size(), arr2.size() <= 10^5
1 <= d <= 10^5
These were the two coding questions I encountered in my IBM OA.
I used AI tool to enhance the post but the question I share is based on my personal recollection I takes some valuable time to write this post so i requested to like ,share and also I am available to answer any comments.
IBM OA Experience – On Campus-Two Coding Questions
Interview Questions (2)
Make the Array Alternating in Parity
Given an array of positive integers, find the minimum number of operations required to make the array alternate in parity. The final array can follow either pattern: Odd, Even, Odd, Even,... or Even, Odd, Even, Odd,... In one operation you may replace any element with floor(nums[i] / 2) and you may apply multiple operations to the same element. Constraints: 1 <= nums.size() < 10^5, nums[i] > 0.
Maximum Number of Valid Pairs
Given two arrays arr1 and arr2 and an integer d, form the maximum number of pairs (arr1[i], arr2[j]) such that arr1[i] <= arr2[j] <= arr1[i] + d. Each element can be used in at most one pair. Constraints: 1 <= arr1.size(), arr2.size() <= 10^5, 1 <= d <= 10^5.