Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

73 Algorithm Practice Problems That Landed Me A FAANG Internship, Exercises of Data Structures and Algorithms

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

2020/2021

Available from 02/16/2022

abracadabrar
abracadabrar 🇨🇦

1 document

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The 73 Algorithm
Problems That Landed
Me A F A A N G Internship
Understanding > Quantity
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?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download 73 Algorithm Practice Problems That Landed Me A FAANG Internship and more Exercises Data Structures and Algorithms in PDF only on Docsity!

The 73 Algorithm

Problems That Landed

Me A FAANG Internship

Understanding > Quantity

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.

1. Is Number Palindrome

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

2. Rotate an NxN Matrix (In Place)

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

3. Balanced Parentheses Problem

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

6. Even Odd Partition

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

7. Phone Number Mnemonics

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

8. Test If A Binary Tree Is Symmetric

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

9. Lowest Common Ancestor In A BST

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

10. Sum Root To Leaf Paths

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

13. Count Primes

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

14. Two Sum

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

15. Two Sum II (Sorted Input)

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

16. Three Sum

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

19. Find Minimum Time Difference

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

20. Power Of Four

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

21. Longest Palindrome Construction

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

22. Count Subarrays That Sum to K

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

23. Spiral Traversal of An MxN Matrix

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

24. Number Of Ways To Traverse A Matrix

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

27. Replace Words With Shortest Matching Prefix

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

28. Word Subsets

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

29. Kth Smallest Element In BST

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

30. Tree Reconstruction

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

31. Binary Tree Diameter

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

34. Best Time to Buy and Sell Stock II

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

35. Contains Duplicate

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

36. Contains Duplicate II

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

37. Product Of Array Except Self

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

38. Container With Most Water

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]).

41. Search Rotated Sorted Array

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

42. Nth Tribonacci Number

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

43. Climbing Stairs

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

44. Min-Cost Climbing Stairs

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

45. Change Making Problem

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.