PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Palantir
  • Coding & Algorithms
  • Software Engineer

Solve two algorithm problems from a menu

Company: Palantir

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Choose two problems from an algorithms menu (arrays, strings, hash maps, graphs). For each: 1) state clarifying assumptions, 2) implement a correct solution in your preferred language, 3) analyze time and space complexity, 4) provide tests for edge cases and large inputs, and 5) discuss alternative approaches and trade-offs.

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

Given an unsorted list of integers, return the length of the longest sequence of consecutive integer values. The values do not need to appear next to each other in the original list. Duplicate numbers do not extend a sequence. Assume an empty list returns 0.

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 best

Time complexity: O(n) average time, where n is the number of elements in nums.. Space complexity: O(n).

Hints

  1. Fast membership checks are useful when repeatedly asking whether x - 1 or x + 1 exists.
  2. You only need to start counting from numbers that are the beginning of a sequence.

Part 2: Number of Islands

Given a rectangular grid of 0s and 1s, count how many islands it contains. An island is a maximal group of 1 cells connected horizontally or vertically. Diagonal cells do not connect islands. Assume an empty grid, or a grid with zero columns, contains 0 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 islands

Time complexity: O(r * c), where r is the number of rows and c is the number of columns.. Space complexity: O(r * c).

Hints

  1. Treat each land cell as a node in a graph and explore its connected component.
  2. Each time you discover an unvisited land cell, traverse its whole island and increment the answer once.
Last updated: Apr 22, 2026

Related Coding Questions

  • Find Shortest Paths in Road Network - Palantir (easy)
  • Group employees by shared interests - Palantir (Medium)

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.