Meta

49 DSA problems asked in Meta coding interviews

23 Easy
26 Medium
0 Hard
​

49 problems

Arrays & Hashing
2
Max Consecutive Ones
Easy

Given a binary array nums, return the length of the longest run of consecutive 1s in it.

Find Pivot Index
Easy

Given an integer array nums, return the pivot index: the leftmost index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right. An empty side sums to 0. If no such index exists, return -1.

Backtracking
1
Permutations II
Medium

Given an array nums that may contain duplicate values, return every distinct permutation of it. The permutations can be returned in any order.

Binary Search
2
Peak Index in a Mountain Array
Medium

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
Medium

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 Manipulation
1
Divide Two Integers
Medium

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.

Design
2
Range Sum Query - Immutable
Easy

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)
Medium

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 Programming
1
N-th Tribonacci Number
Easy

The Tribonacci sequence is defined by T(0) = 0, T(1) = 1, T(2) = 1, and T(n + 3) = T(n) + T(n + 1) + T(n + 2) for n >= 0. Given n, return the value of T(n).

Graphs
1
01 Matrix
Medium

Given an m x n binary matrix mat, return a matrix of the same size where each cell holds the distance to the nearest 0. Two cells sharing an edge are at distance 1.

Greedy
2
Increasing Triplet Subsequence
Medium

Given an integer array nums, return true if there are indices i < j < k with nums[i] < nums[j] < nums[k]. If no such three indices exist, return false.

Can Place Flowers
Easy

A long flowerbed is given as an array of 0s (empty plot) and 1s (planted plot). Flowers cannot be planted in adjacent plots. Given the flowerbed and an integer n, return true if n new flowers can be planted without breaking the no-adjacent-flowers rule, and false otherwise.

Intervals
1
Summary Ranges
Easy

You are given a sorted array of unique integers `nums`. Cover every number with the smallest sorted list of ranges, where a range [a,b] holds every integer from a to b. Write a range as "a->b" when a != b and as "a" when a == b.

Linked List
3
Remove Duplicates from Sorted List II
Medium

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
Medium

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
Medium

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.

Math
1
Add Strings
Easy

Given two non-negative integers num1 and num2 written as strings, return their sum as a string. You may not use a big-integer library or convert the whole strings to integers directly.

Sliding Window
3
Maximum Average Subarray I
Easy

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
Medium

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.

Max Consecutive Ones III
Medium

Given a binary array `nums` and an integer `k`, return the maximum number of consecutive `1`s in the array if you can flip at most `k` `0`s to `1`s.

Sorting
2
H-Index
Medium

You are given an array `citations` where `citations[i]` is how many times a researcher's i-th paper has been cited. Return the researcher's h-index: the largest h such that the researcher has at least h papers that were each cited at least h times.

Sort an Array
Medium

Given an array of integers `nums`, sort it in ascending order and return it. Do not use any built-in sort: the solution must run in O(n log n) time with as little extra space as possible.

Stack
6
Implement Queue using Stacks
Easy

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
Medium

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
Medium

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
Medium

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
Easy

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.

Remove All Adjacent Duplicates In String
Easy

You are given a string `s` of lowercase English letters. A duplicate removal picks two adjacent, equal letters and deletes both. Repeat duplicate removals until no more can be made, and return the final string. The answer is unique.

Strings
6
Reverse Words in a String III
Easy

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
Easy

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)
Medium

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
Medium

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
Medium

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.

Merge Strings Alternately
Easy

You are given two strings word1 and word2. Merge them by adding letters in alternating order, starting with word1. If one string is longer than the other, append the additional letters onto the end of the merged string. Return the merged string.

Trees
9
Binary Tree Inorder Traversal
Easy

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
Easy

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
Easy

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
Medium

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
Medium

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
Easy

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
Easy

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
Easy

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
Medium

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 Pointers
6
Remove Duplicates from Sorted Array II
Medium

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
Easy

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
Easy

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
Medium

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.

Backspace String Compare
Easy

Given two strings s and t, return true if they are equal when both are typed into empty text editors, where '#' means a backspace character. Backspacing an empty text leaves it empty. Follow-up: solve it in O(n) time and O(1) space.

3Sum Closest
Medium

Given an integer array `nums` of length `n` and an integer `target`, find three integers at distinct indices in `nums` whose sum is closest to `target`. Return that sum. You may assume each input has exactly one closest sum.