Solve four OA coding problems
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: HR Screen
Quick Answer: This set of four problems evaluates core algorithmic competencies including array manipulation and counting in sorted arrays, resource-constrained optimization for reachability with recharge stations, grid-based connected-component identification, and detection of longest consecutive-value sequences, emphasizing data-structure selection, edge-case reasoning, and time/space complexity analysis. Commonly asked in Coding & Algorithms interviews to assess both conceptual understanding of algorithmic paradigms and practical application of efficient techniques under constraints, these questions measure algorithmic thinking, complexity analysis, and problem decomposition.
Part 1: Compare Negative and Positive Counts
Constraints
- 0 <= len(nums) <= 100000
- -100000 <= nums[i] <= 100000
- nums is sorted in nondecreasing order
Examples
Input: ([-2, -1, -1, 1, 2, 3],)
Expected Output: 3
Explanation: There are 3 negative values and 3 positive values, so the answer is 3.
Input: ([-3, -2, -1, 0, 0, 1, 2],)
Expected Output: 3
Explanation: There are 3 negative values and 2 positive values.
Input: ([0, 0, 0],)
Expected Output: 0
Explanation: Zeros are ignored, so both counts are 0.
Input: ([-5],)
Expected Output: 1
Explanation: The single element is negative.
Input: ([],)
Expected Output: 0
Explanation: An empty array has no negative or positive values.
Hints
- Because the array is sorted, all negative numbers appear before all zeros and positive numbers.
- Use binary search to find the first 0 and the first value greater than 0.
Part 2: Reach a Destination with Charging Stations
Constraints
- 0 <= target <= 1000000000
- 0 <= startCharge <= 1000000000
- 0 <= len(stations) <= 100000
- 0 <= position_i <= target
- 0 <= charge_i <= 1000000000
- stations is sorted by position_i in nondecreasing order
Examples
Input: (100, 10, [[10, 60], [20, 30], [30, 30], [60, 40]])
Expected Output: 2
Explanation: Stop at position 10 for 60 charge, then later use the station at position 60 for 40 charge.
Input: (100, 50, [[25, 25], [50, 50]])
Expected Output: 1
Explanation: The drone can reach position 50, then one charge of 50 is enough to reach target.
Input: (1, 1, [])
Expected Output: 0
Explanation: The starting charge is already enough.
Input: (100, 1, [[10, 100]])
Expected Output: -1
Explanation: The first station is too far away to reach.
Input: (10, 0, [[0, 5], [5, 5]])
Expected Output: 2
Explanation: The drone starts at a station at position 0, charges there, then charges again at position 5.
Hints
- As you move as far as possible, keep track of all stations you have already passed but not used.
- When you cannot go farther, choose the passed station with the largest available charge.
Part 3: Count Objects Placed on a Grid
Constraints
- 0 <= m <= 500
- 0 <= n <= 500
- grid contains only 'X' and '.'
- All rows have the same length
- Each object is a straight horizontal or vertical group of 'X' cells
- No two distinct objects touch horizontally or vertically
Examples
Input: ([],)
Expected Output: 0
Explanation: An empty grid has no objects.
Input: (['XX..', '....', '..X.', '..X.'],)
Expected Output: 2
Explanation: There is one horizontal object in the first row and one vertical object in the last column area.
Input: (['X.X', '...', 'XXX'],)
Expected Output: 3
Explanation: There are two single-cell objects in the first row and one horizontal object in the last row.
Input: (['...', '...'],)
Expected Output: 0
Explanation: There are no 'X' cells.
Input: (['X'],)
Expected Output: 1
Explanation: A single 'X' cell is one object.
Hints
- Instead of exploring every full object, count only the first cell of each object.
- An 'X' cell starts a new object if there is no 'X' directly above it and no 'X' directly to its left.
Part 4: Find the Longest Consecutive-Value Run
Constraints
- 0 <= len(nums) <= 100000
- -1000000000 <= nums[i] <= 1000000000
- nums may contain duplicates
- The expected solution should run in O(n) average time
Examples
Input: ([100, 4, 200, 1, 3, 2],)
Expected Output: 4
Explanation: The longest consecutive run is 1, 2, 3, 4.
Input: ([1, 2, 0, 1],)
Expected Output: 3
Explanation: Duplicates are ignored; the run 0, 1, 2 has length 3.
Input: ([],)
Expected Output: 0
Explanation: An empty array has no consecutive run.
Input: ([5],)
Expected Output: 1
Explanation: A single number forms a run of length 1.
Input: ([-2, -1, 0, 2, 3],)
Expected Output: 3
Explanation: The longest run is -2, -1, 0.
Hints
- Use a set so that membership checks are O(1) on average.
- Only start counting from a number x if x - 1 is not present.