Meta
49 DSA problems asked in Meta coding interviews
49 problems
Arrays & Hashing2
Backtracking1
Binary Search2
Peak Index in a Mountain Array
You are given an integer mountain array `arr` of length n: the values strictly increase up to some peak and then strictly decrease. Return the index of the peak element. Your solution must run in O(log n) time.
Search in Rotated Sorted Array II
An array sorted in non-decreasing order, possibly with repeated values, has been rotated at some unknown pivot k, so it reads [nums[k], ..., nums[n-1], nums[0], ..., nums[k-1]]. Given the rotated array nums and an integer target, return true if target appears in nums and false otherwise. Aim to do as few operations as possible.
Bit Manipulation1
Divide Two Integers
Given two integers dividend and divisor, divide them without using multiplication, division or the mod operator. The quotient is truncated toward zero, so 8.345 becomes 8 and -2.7335 becomes -2. Assume the environment only stores signed 32-bit integers: if the quotient is above 2^31 - 1 return 2^31 - 1, and if it is below -2^31 return -2^31.
Design2
Range Sum Query - Immutable
Given an integer array nums, answer many queries of the form: what is the sum of nums[left] through nums[right], inclusive? Implement NumArray with a constructor NumArray(nums) and a method sumRange(left, right). The array never changes after construction.
Insert Delete GetRandom O(1)
Implement RandomizedSet: insert(val) adds val if absent and returns true, otherwise false; remove(val) removes val if present and returns true, otherwise false; getRandom() returns a uniformly random element (at least one element is present). Every operation must run in average O(1) time. Here getRandom is only called when the set holds exactly one element, so the output is deterministic.
Dynamic Programming1
Graphs1
Greedy2
Intervals1
Linked List3
Remove Duplicates from Sorted List II
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
Reverse Linked List II
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Delete the Middle Node of a Linked List
You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list. The middle node of a linked list of size n is the floor(n / 2)th node from the start using 0-based indexing. For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
Math1
Sliding Window3
Maximum Average Subarray I
You are given an integer array `nums` of n elements and an integer `k`. Find a contiguous subarray of length exactly `k` that has the maximum average value and return that average. Answers within $10^{-5}$ of the true value are accepted.
Fruit Into Baskets
You walk along a single row of fruit trees, where `fruits[i]` is the type of fruit the i-th tree produces. You have two baskets, and each basket can hold any amount of fruit but only one fruit type. Starting at any tree, you pick exactly one fruit from every tree while moving right, and you must stop as soon as a tree has a fruit type that fits in neither basket. Return the maximum number of fruits you can pick.
Sorting2
Stack6
Implement Queue using Stacks
Build a first-in-first-out queue using only two stacks. Support push (add to the back), pop (remove from the front), peek (read the front) and empty, using only standard stack operations.
Simplify Path
You are given an absolute Unix-style path that starts with a slash. Return its simplified canonical form. A single period `.` means the current directory, a double period `..` moves up to the parent directory, and several consecutive slashes count as one. Any other name, including names made only of periods such as `...`, is a regular directory name. The canonical path starts with one slash, separates directory names with exactly one slash, does not end with a slash (unless it is the root `/`), and contains no `.` or `..` parts.
Minimum Remove to Make Valid Parentheses
Given a string s made of '(', ')' and lowercase English letters, remove the minimum number of parentheses (at any positions) so that the resulting string is valid, and return it. A string is valid if it is empty or contains only lowercase letters, or it can be written as AB where A and B are valid, or as (A) where A is valid.
Basic Calculator II
Given a string `s` holding a valid expression of non-negative integers, the operators `+`, `-`, `*`, `/` and spaces, evaluate it and return its value. Multiplication and division bind tighter than addition and subtraction, and integer division truncates toward zero. You may not use a built-in expression evaluator such as `eval()`.
Next Greater Element I
The next greater element of a value in an array is the first value to its right that is larger. You are given two arrays of distinct integers, `nums1` and `nums2`, where `nums1` is a subset of `nums2`. For each `nums1[i]`, find the same value in `nums2` and return its next greater element there, or `-1` if there is none.
Strings6
Reverse Words in a String III
Given a string s of words separated by single spaces, reverse the characters of each word while keeping the words in their original order and the spaces in place. Return the resulting string.
Greatest Common Divisor of Strings
A string t divides a string s if s is t concatenated with itself one or more times. Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2, or an empty string if there is none.
String to Integer (atoi)
Implement myAtoi(s), which converts a string to a 32-bit signed integer: skip leading spaces, read an optional + or - sign, then read digits until a non-digit or the end of the string (no digits means 0). Clamp the value to the range [-2^31, 2^31 - 1] and return it.
Count and Say
The count-and-say sequence starts with "1". Each later term describes the previous one out loud: split it into runs of equal digits, and for each run write how many digits it has followed by the digit. For example "3322251" reads as two 3s, three 2s, one 5, one 1, giving "23321511". Given n, return the nth term.
Reverse Words in a String
Given a string s, reverse the order of its words. A word is a maximal run of non-space characters. The result must contain the words in reverse order separated by exactly one space: leading and trailing spaces are removed, and runs of several spaces between words collapse to a single space.
Trees9
Binary Tree Inorder Traversal
Given the root of a binary tree, return the inorder traversal of its node values: the left subtree, then the node itself, then the right subtree.
Range Sum of BST
Given the root of a binary search tree and two integers low and high, return the sum of the values of all nodes whose value lies in the inclusive range [low, high].
Count Complete Tree Nodes
Given the root of a complete binary tree, return the number of nodes in the tree. In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. The last level $h$ can hold between $1$ and $2^h$ nodes. Design an algorithm that runs in less than $O(n)$ time.
Construct Binary Tree from Inorder and Postorder Traversal
Given two integer arrays `inorder` and `postorder`, the inorder and postorder traversals of the same binary tree with unique values, rebuild the tree and return its root.
Sum Root to Leaf Numbers
Every node of a binary tree holds one digit from 0 to 9. Each root-to-leaf path spells a number by reading its digits from the root down (the path 1 -> 2 -> 3 spells 123). Return the sum of the numbers spelled by all root-to-leaf paths. A leaf is a node with no children.
Merge Two Binary Trees
You are given two binary trees `root1` and `root2`. Imagine laying one on top of the other: some positions are covered by both trees, some by only one. Merge them into a new tree. Where both trees have a node, the merged node holds the sum of the two values. Where only one tree has a node, that node is used as is. Return the merged tree. The merge starts from the roots of both trees.
Binary Tree Preorder Traversal
Given the root of a binary tree, return the preorder traversal of its nodes' values: visit a node, then its whole left subtree, then its whole right subtree.
Binary Tree Postorder Traversal
Given the root of a binary tree, return the postorder traversal of its nodes' values: traverse the left subtree, then the right subtree, then visit the node itself.
Insert into a Binary Search Tree
You are given the `root` of a binary search tree (BST) and a value `val` to insert. Return the root of the BST after the insertion. It is guaranteed that `val` does not already exist in the tree. Several valid BSTs can hold the same values. This problem expects the standard insertion: walk down from the root, going left when `val` is smaller than the current node and right when it is larger, and attach the new node as a **leaf** at the first empty child you reach. No existing node moves.
Two Pointers6
Remove Duplicates from Sorted Array II
Given an integer array nums sorted in non-decreasing order, remove duplicates in place so that each distinct value appears at most twice, keeping the relative order. Put the kept values in the first k slots of nums and return k. Use O(1) extra memory.
Reverse Vowels of a String
Given a string s, reverse only the vowels in it and return the result. The vowels are 'a', 'e', 'i', 'o' and 'u', in lower or upper case, and they can appear more than once. All other characters stay where they are.
Is Subsequence
Given two strings s and t, return true if s is a subsequence of t, and false otherwise. A subsequence is what remains after deleting zero or more characters from a string without changing the order of the rest; for example "ace" is a subsequence of "abcde" but "aec" is not.
String Compression
Given an array of characters chars, compress it in place: for each group of consecutive repeating characters, write the character, followed by the group length if it is greater than 1 (lengths of 10 or more take several characters). Return the new length. Use only constant extra space.