Solve four OA coding problems
MetaSoftware Engineer · HR ScreenLast updated
This set of coding tasks evaluates fundamental skills in array manipulation, graph traversal, greedy strategy, and sequence analysis.
1. Sorted Frequency
Given a sorted array arr and a target value k, return the count of occurrences of k in arr. Optimize for O(log n) time.
Example: arr = [1, 2, 2, 2, 3], k = 2 -> Output: 3
2. Minimum Recharges
You have a distance D to travel and a vehicle with battery capacity C. You start with a full battery. There are recharge stations at positions stations = [s1, s2, ..., sn]. Return the minimum number of recharges needed to reach D, or -1 if impossible.
Example: D = 10, C = 6, stations = [3, 6, 8] -> Output: 2 (Charge at 3 and 8).
3. Island Count
Given a 2D binary grid of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and formed by connecting adjacent lands horizontally or vertically.
Example: grid = [['1','1','0'], ['1','0','0'], ['0','0','1']] -> Output: 2
4. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. The algorithm must run in O(n) time.
Example: nums = [100, 4, 200, 1, 3, 2] -> Output: 4 (Sequence: [1, 2, 3, 4])
Constraints
- For problems 1, 3, and 4:
nup to 10^5. - For problem 2: Distance and capacity are positive integers up to 10^9.
Hints
Hint 1
For problem 1, use binary search to find the first and last index of the target value.
Hint 2
For problem 2, at each point, travel as far as possible before needing a recharge using a greedy approach.
Hint 3
For problems 3 and 4, consider using a Disjoint Set Union (DSU) or a Hash Set to track visited states and values efficiently.
Follow-up questions
- How would you modify the island count if diagonals were also considered connected?
- What is the space complexity of your longest consecutive sequence solution, and can it be improved?
- In the recharge problem, what if the stations have different charging costs rather than just availability?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.