Solve 15 common interview problems
AppleSoftware Engineer · Technical ScreenLast updated
This collection evaluates algorithmic problem-solving skills, mastery of fundamental data structures (arrays, strings, hash maps, stacks, queues, trees, graphs, linked lists) and the ability to reason about time and space complexity.
Below are fifteen independent coding problems commonly discussed in software engineering interviews. For each one, write an efficient algorithm and be prepared to explain the time and space complexity, as interviewers may ask for optimizations or follow-up improvements.
-
Find a target-sum pair. Given an integer array
numsand an integertarget, return the indices of two distinct elements whose values add up totarget. You may assume exactly one valid answer exists, and you may return the indices in any order. -
Count contiguous segments with sum
k. Given an integer arraynumsand an integerk, return the number of contiguous subarrays whose sum is exactlyk. -
Build a product array without division. Given an integer array
nums, return an arrayanswerwhereanswer[i]is the product of all elements ofnumsexceptnums[i]. Do not use division, and aim for linear time. -
Find the longest substring with all unique characters. Given a string
s, return the length of the longest contiguous substring that contains no repeated characters. -
Find the shortest covering substring. Given strings
sandt, return the smallest substring ofsthat contains every character oft, including duplicates. If no such substring exists, return an empty string. -
Expand a nested repetition encoding. Given a string encoded with rules such as
k[encoded_part], where the substring inside brackets should be repeatedktimes, return the fully decoded string. For example,3[a2[c]]becomesaccaccacc. -
Count connected land regions in a grid. Given a 2D grid of characters where
'1'represents land and'0'represents water, return the number of islands. Cells are connected horizontally and vertically. -
Find the shortest word transformation length. Given a
beginWord, anendWord, and a dictionarywordList, transform the begin word into the end word by changing exactly one letter at a time. Every intermediate word must exist inwordList. Return the length of the shortest valid transformation sequence, or0if no such sequence exists. -
Determine whether all courses can be completed. You are given
numCourseslabeled from0tonumCourses - 1and a list of prerequisite pairs[a, b], meaning coursebmust be taken before coursea. Returntrueif it is possible to finish all courses, otherwise returnfalse. -
Traverse a binary tree by levels. Given the root of a binary tree, return the node values level by level from top to bottom, where each level is returned as a separate list.
-
Find the nearest shared ancestor in a binary tree. Given the root of a binary tree and two nodes
pandq, return their lowest common ancestor. The lowest common ancestor is the deepest node that has bothpandqas descendants, where a node may be a descendant of itself. -
Merge multiple sorted linked lists. You are given an array of
ksingly linked lists, each sorted in ascending order. Merge them into one sorted linked list and return its head. -
Deep-copy a linked list with arbitrary cross-links. Each node in a linked list has
val,next, andrandompointers, whererandommay point to any node in the list or benull. Return a deep copy of the entire list. -
Rotate a square matrix in place. Given an
n x nmatrix, rotate it 90 degrees clockwise without using another matrix for the final result. -
Compute the longest increasing subsequence length. Given an integer array
nums, return the length of the longest strictly increasing subsequence. Aim for an algorithm better thanO(n^2)if possible.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.