DRW Intern OA (3 Qs) [USA]

I just took DRW OA after today's contest ended. Hopefully I will finally become a guardian after ranking gets updated. I wonder how the top 500 Motorola Mobility Referral works...


Outline

There are in total of 3 questions and we are given 150 minutes, which is wayyyyyyyyyyyy more than enough.
The test is conducted on Codility and the DRW encourges that we use C++, Java, Python even though they do support other languages.
I decided to code some problems in Java and some in C++.
Note that Codility does not have any library included by default. You have to memorize what library to include.
The highest Java version they have is 11, and C++14.


Question 1

Given A and B, find the number of integer X such that X*(X+1) falls between [A, B], both inclusive.

Constraint:
1 <= A <= B <= 1e9

My Thoughts:
Unclear problem statement! Could X be negative??
I coded up the bruteforce O(sqrt(N)) solution because I think that should be good enough, and I found out from the testcases that X can't be negative.
We can also give a O(logN) solution with binary search but I felt it unncessary during OA as that would require me to write a separate function and call solve(B) - solve(A-1)

Passed all sample testcases in Java 11.



Question 2

Given M units of 1 by 1 square, and N units of 2 by 2 squares. Find the maximum size of square that you can make with them.

Constraints:
0 <= M <= 1e9
0 <= N <= 1e9

My Solution

Greedily use as many as 2x2 as possible, and fill that gap with 1x1.

  • If we can fill the gap, then binary search for how much the remaining 1x1 can improve it.
  • If we can't fill the gap, backtrack the 2x2 length by 1 unit, and do binary search for all 1x1.

Passed all sample testcases in C++14.



Question 3

Given an array A and B of the same length, we have to create an array C, C[i] can be either A[i] or B[i], such that the MEX (Minimum Excluded Positive Integer) of C is minimized.
Return the MEX of C.

Constraints:
1 <= length of A/B <= 100000
1 <= A[i], B[i] <= 1e9

My Solution
Observe that for any pair of (A[i], B[i]), we have an option to avoid a certain number unless A[i] == B[i].
Let N = length of A, create an has bool array of length N+2 and mark value as present or not.
If A[i] == B[i], then has[A[i]] = true.
After we mark it all, use a for (int ans = 1; true; ++ans) and return the first element not present in has.

Passed all sample testcases in C++14.

Comments (2)