PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in array and matrix manipulation, including spiral traversal with boundary and edge-case handling, index management, hash-based complement lookup for pair-sum, and reasoning about time and space complexity.

  • medium
  • Illumio
  • Coding & Algorithms
  • Software Engineer

Solve Matrix Traversal and Pair Sum

Company: Illumio

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You will solve two independent coding problems. ### Problem 1: Traverse a Matrix in Spiral Order Given an `m x n` matrix of integers, return all elements in clockwise spiral order, starting from the top-left cell. **Example** Input: ```text matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] ``` Output: ```text [1, 2, 3, 6, 9, 8, 7, 4, 5] ``` **Requirements** - Handle empty matrices. - Handle non-square matrices. - Time complexity should be `O(m * n)`. ### Problem 2: Find Two Numbers That Sum to Target Without Sorting Given an unsorted array of integers `nums` and an integer `target`, return the indices of two distinct elements whose values add up to `target`. You may assume there is exactly one valid answer. You are not allowed to sort the input array because the original indices must be preserved. **Example** Input: ```text nums = [8, 1, 5, 3, 9], target = 12 ``` Output: ```text [3, 4] ``` Explanation: `nums[3] + nums[4] = 3 + 9 = 12`. **Requirements** - Do not sort the array. - Return original indices. - Aim for `O(n)` time complexity.

Quick Answer: This question evaluates proficiency in array and matrix manipulation, including spiral traversal with boundary and edge-case handling, index management, hash-based complement lookup for pair-sum, and reasoning about time and space complexity.

Part 1: Traverse a Matrix in Spiral Order

Given an m x n matrix of integers, return all elements in clockwise spiral order starting from the top-left cell. The traversal should move right across the top row, down the rightmost column, left across the bottom row, and up the leftmost column, then repeat inward until every element has been visited.

Constraints

  • 0 <= m <= 200
  • 0 <= n <= 200
  • If matrix is non-empty, every row has the same length n.
  • -10^9 <= matrix[i][j] <= 10^9
  • The algorithm should run in O(m * n) time.

Examples

Input: ([[1, 2, 3], [4, 5, 6], [7, 8, 9]],)

Expected Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]

Explanation: A square 3 x 3 matrix is traversed around the outside, then the center element is added.

Input: ([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],)

Expected Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]

Explanation: Handles a non-square matrix with more columns than rows.

Input: ([[1, 2, 3, 4]],)

Expected Output: [1, 2, 3, 4]

Explanation: Single-row matrix is already in spiral order.

Input: ([[1], [2], [3]],)

Expected Output: [1, 2, 3]

Explanation: Single-column matrix is traversed from top to bottom.

Input: ([],)

Expected Output: []

Explanation: Empty matrix has no elements to return.

Hints

  1. Track four boundaries: top, bottom, left, and right. After traversing one side, move that boundary inward.
  2. Before traversing the bottom row or left column, check that the boundaries have not crossed to avoid duplicates.

Part 2: Find Two Numbers That Sum to Target Without Sorting

Given an unsorted array of integers nums and an integer target, return the original indices of two distinct elements whose values add up to target. You may assume there is exactly one valid answer. You must not sort the array because the original indices must be preserved. Return the indices in increasing order.

Constraints

  • 2 <= len(nums) <= 100000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Exactly one valid pair exists.
  • Do not sort or mutate nums.

Examples

Input: ([8, 1, 5, 3, 9], 12)

Expected Output: [3, 4]

Explanation: nums[3] + nums[4] = 3 + 9 = 12.

Input: ([2, 7, 11, 15], 9)

Expected Output: [0, 1]

Explanation: The first two elements sum to 9.

Input: ([3, 3], 6)

Expected Output: [0, 1]

Explanation: Minimum-size input with duplicate values; the two indices must be distinct.

Input: ([-4, 10, 6, 2], 8)

Expected Output: [2, 3]

Explanation: Handles negative and positive values. nums[2] + nums[3] = 6 + 2 = 8.

Input: ([0, -1, 4, 9], -1)

Expected Output: [0, 1]

Explanation: Handles zero and a negative target.

Hints

  1. For each number x, compute the complement target - x and check whether that complement has appeared earlier.
  2. Use a hash map from value to index to preserve original indices while achieving O(n) time.
Last updated: Jun 13, 2026

Related Coding Questions

  • Count Regions in a Binary Grid - Illumio (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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.