Solve two algorithm problems from a menu
Company: Palantir
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving, practical coding implementation, complexity analysis, testing rigor, and the ability to discuss design trade-offs across topics such as arrays, strings, hash maps, and graphs.
Part 1: Longest Consecutive Sequence
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- The input may contain duplicate values
- Return 0 when the input list is empty
Examples
Input: ([100, 4, 200, 1, 3, 2],)
Expected Output: 4
Explanation: The longest consecutive sequence is [1, 2, 3, 4], so the answer is 4.
Input: ([0, 3, 7, 2, 5, 8, 4, 6, 0, 1],)
Expected Output: 9
Explanation: The numbers 0 through 8 are all present, giving a longest sequence length of 9.
Hints
- Fast membership checks are useful when repeatedly asking whether x - 1 or x + 1 exists.
- You only need to start counting from numbers that are the beginning of a sequence.
Part 2: Number of Islands
Constraints
- 0 <= number of rows <= 1000
- 0 <= number of columns <= 1000
- For non-empty grids, rows * columns <= 200000
- The grid is rectangular
- Connectivity is 4-directional only: up, down, left, right
Examples
Input: (([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 1]]),)
Expected Output: 3
Explanation: There are three separate groups of connected land cells.
Input: ([],)
Expected Output: 0
Explanation: An empty grid contains no land and therefore no islands.
Hints
- Treat each land cell as a node in a graph and explore its connected component.
- Each time you discover an unvisited land cell, traverse its whole island and increment the answer once.