

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A compiled list of handpicked algorithm problems to promote fluid understanding of data structures and algorithms. Work through this list honestly, and you won't need to do much more to prepare for an internship interview.
Typology: Exercises
1 / 33
This page cannot be seen from the preview
Don't miss anything!
FAANG internship interviews are intimidating. Preparing for them can be stressful. This stress is compounded by your friendly campus tryhard repeatedly telling you about how many Leetcode problems they solved last night. Well, I’ve got news for you. The number of problems you solve is largely irrelevant to your performance in interviews. Instead, it is important to focus on your understanding of the underlying concepts of these questions, as you will likely never see the same one twice. The enclosed questions are hand-picked to challenge and reform your conceptions of common data structures and problem solving techniques. It’s going to take a lot of patience, but as you work through these problems, you will become comfortable with your own skill set and begin to appreciate that at the end of the day, these questions are genuinely just fun little puzzles. Who doesn’t love puzzles?
The following problems are not sorted by difficulty. If you get stuck on a question, don’t panic. Just come back to it later.
Given an integer x, return true if x is a palindrome integer. An integer is a palindrome when it reads the same backward as forward. For example, 121 is a palindrome while 123 is not.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Primitive manipulation Try It: Leetcode
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Target Runtime: O(N^2 ) Target Space Complexity: O(1) Concepts: Creativity with matrices, looping Try It: Leetcode
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The matching should cover the entire input string (not partial).
Target Runtime: O(n * length) Target Space Complexity: O(1) Concepts: String manipulation, recursion, dynamic programming Try It: Leetcode
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Try It: Leetcode
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Target Runtime: O(4digits)
Target Space Complexity: O(digits) Concepts: Try It: Leetcode
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Target Runtime: O(n) Target Space Complexity: O(height) Concepts: BST, recursion, iteration Try It: Leetcode
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. The lowest common ancestor is defined between two nodes p and q as the lowest node in the tree that has both p and q as descendants (where we allow a node to be a descendant of itself).
Target Runtime: O(logn) Target Space Complexity: O(logn) Concepts: BST, recursion, iteration Try It: Leetcode
You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number.
Given the sorted rotated array nums of unique elements, return the minimum element of this array.
Target Runtime: O(logn) Target Space Complexity: O(1) Concepts: arrays, binary search Try It: Leetcode
Given an integer n, return the number of prime numbers that are strictly less than n.
Target Runtime: O(nloglogn) Target Space Complexity: O(n) Concepts: tricks with numbers Try It: Leetcode
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Target Runtime: O(n) Target Space Complexity: O(n) Concepts: Hashing Try It: Leetcode
Given a 1-indexed array of integer numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. The tests are generated such that there is exactly one solution. You may not use the same element twice.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: arrays, sorting, pointer manipulation Try It: Leetcode
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets.
Target Runtime: O(n^2 ) Target Space Complexity: O(1) Concepts: nested loops Try It: Leetcode
Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.
Target Runtime: O(1) Target Space Complexity: O(1) Concepts: Alternative representations Try It: Leetcode
Given an integer n, return true if it is a power of four. Otherwise, return false. An integer n is a power of four, if there exists an integer x such that n == 4x.
Target Runtime: O(1) Target Space Complexity: O(1) Concepts: Tricks with numbers Try It: Leetcode
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Greedy, Modulus Try It: Leetcode
Given an array of integers nums and an integer k, return the total number of continuous subarrays whose sum equals to k.
Target Runtime: O(n) Target Space Complexity: O(n) Concepts: Hashing Try It: Leetcode
Given an m x n matrix, return all elements of the matrix in spiral order.
Target Runtime: O(mn) Target Space Complexity: O(1) Concepts: Creativity with matrices, looping Try It: Leetcode
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
Target Runtime: O(mn) Target Space Complexity: O(mn)
Try It: Leetcode
In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word successor. For example, when the root "an" is followed by the successor word "other", we can form a new word "another". Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the successors in the sentence with the root forming it. If a successor can be replaced by more than one root, replace it with the root that has the shortest length. Return the sentence after the replacement.
Target Runtime: O(words^2 ) Target Space Complexity: O(words) Concepts: Hashing Try It: Leetcode
You are given two string arrays words1 and words2. A string b is a subset of string a if every letter in b occurs in a including multiplicity. ● For example, "wrr" is a subset of "warrior" but is not a subset of "world". A string a from words1 is universal if for every string b in words2, b is a subset of a. Return an array of all the universal strings in words1.
Target Runtime: O(1)
Target Space Complexity: O(1) Concepts: Alternative representations Try It: Leetcode
Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: BST, traversal Try It: Leetcode
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
Target Runtime: O(n) Target Space Complexity: O(n) Concepts: BST, traversals, recursion Try It: Leetcode
Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Target Space Complexity: O(1) Concepts: Linear scan, optimization Try It: Leetcode
You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day. Find and return the maximum profit you can achieve.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Greedy, dynamic programming Try It: Leetcode
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Hashing Try It: Leetcode
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Sorting, Hashing, Sliding Window Try It: Leetcode
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Linear scan, tricks with numbers Try It: Leetcode
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
Target Runtime: O(logn) Target Space Complexity: O(1) Concepts: Binary Search Try It: Leetcode
The Tribonacci sequence Tn is defined as follows:
T 0 = 0, T 1 = 1, T 2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
Given n, return the value of Tn.
Target Runtime: O(n) Target Space Complexity: O(n) Concepts: Recursion, Memoization, Dynamic Programming Try It: Leetcode
You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Target Runtime: O(n) Target Space Complexity: O(n) Concepts: Recursion, Memoization, Dynamic Programming Try It: Leetcode
You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0, or the step with index 1. Return the minimum cost to reach the top of the floor.
Target Runtime: O(n) Target Space Complexity: O(1) Concepts: Dynamic Programming Try It: Leetcode
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.