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.
Input: ([],)
Expected Output: 0
Explanation: An empty list has no consecutive sequence.
Input: ([9],)
Expected Output: 1
Explanation: A single element forms a consecutive sequence of length 1.
Input: ([-2, -3, -1, 10],)
Expected Output: 3
Explanation: The longest consecutive sequence is [-3, -2, -1], which has length 3.
Solution
def solution(nums):
num_set = set(nums)
best = 0
for num in num_set:
if num - 1 not in num_set:
current = num
length = 1
while current + 1 in num_set:
current += 1
length += 1
if length > best:
best = length
return bestTime complexity: O(n) average time, where n is the number of elements in nums.. Space complexity: O(n).
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.
Input: (([[0, 0, 0]]),)
Expected Output: 0
Explanation: All cells are water, so the island count is 0.
Input: (([[1]]),)
Expected Output: 1
Explanation: A single land cell forms exactly one island.
Input: (([[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]]),)
Expected Output: 6
Explanation: Diagonal adjacency does not count, so each 1 cell is isolated.
Solution
def solution(grid):
if not grid or not grid[0]:
return 0
rows = len(grid)
cols = len(grid[0])
visited = [[False] * cols for _ in range(rows)]
islands = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and not visited[r][c]:
islands += 1
stack = [(r, c)]
visited[r][c] = True
while stack:
cr, cc = stack.pop()
for nr, nc in ((cr + 1, cc), (cr - 1, cc), (cr, cc + 1), (cr, cc - 1)):
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 1 and not visited[nr][nc]:
visited[nr][nc] = True
stack.append((nr, nc))
return islandsTime complexity: O(r * c), where r is the number of rows and c is the number of columns.. Space complexity: O(r * c).
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.