Practice Problems Flashcards
01 Matrix
Tech Interview Handbook
https://leetcode.com/problems/01-matrix/description/
Imagine a country with several cities. Some cities have direct roads connecting them, while others might be connected through a sequence of intermediary cities. Using a matrix representation, if matrix[i][j] holds the value 1, it indicates that city i is directly linked to city j and vice versa. If it holds 0, then there’s no direct link between them.
Determine the number of separate city clusters (or provinces).
A province is defined as a collection of cities that can be reached from each other either directly or through other cities in the same province.
Examples
Input:
[[1,1,0],[1,1,0],[0,0,1]]
Expected Output:
2
Justification:
There are two provinces: cities 0 and 1 form one province, and city 2 forms its own province.
Input:
[[1,0,0,1],[0,1,1,0],[0,1,1,0],[1,0,0,1]]
Expected Output:
2
Justification:
There are two provinces: cities 0 and 3 are interconnected forming one province, and cities 1 and 2 form another.
Input:
[[1,0,0],[0,1,0],[0,0,1]]
Expected Output:
3
Justification:
Each city stands alone and is not connected to any other city. Thus, we have three provinces.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/651a8380aa7cf9c7c20f9aa8
Given an array with positive numbers and a positive target number, find all of its contiguous subarrays whose product is less than the target number.
Example 1:
Input: [2, 5, 3, 10], target=30
Output: [2], [5], [2, 5], [3], [5, 3], [10]
Explanation: There are six contiguous subarrays whose product is less than the target.
Example 2:
Input: [8, 2, 6, 5], target=50
Output: [8], [2], [8, 2], [6], [2, 6], [5], [6, 5]
Explanation: There are seven contiguous subarrays whose product is less than the target.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638f976d20f87893374e4e6b
Given an array arr of unsorted numbers and a target sum, count all triplets in it such that arr[i] + arr[j] + arr[k] < target where i, j, and k are three different indices. Write a function to return the count of such triplets.
Example 1:
Input: [-1, 0, 2, 3], target=3
Output: 2
Explanation: There are two triplets whose sum is less than the target: [-1, 0, 3], [-1, 0, 2]
Example 2:
Input: [-1, 4, 2, 1, 3], target=5
Output: 4
Explanation: There are four triplets whose sum is less than the target:
[-1, 1, 4], [-1, 1, 3], [-1, 1, 2], [-1, 2, 3]
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638f7a0a4544c65f117ba260
We are given an unsorted array containing ‘n’ numbers taken from the range 1 to ‘n’. The array originally contained all the numbers from 1 to ‘n’, but due to a data error, one of the numbers got duplicated which also resulted in one number going missing. Find both these numbers.
Example 1:
Input: [3, 1, 2, 5, 2]
Output: [2, 4]
Explanation: ‘2’ is duplicated and ‘4’ is missing.
Example 2:
Input: [3, 1, 2, 3, 6, 4]
Output: [3, 5]
Explanation: ‘3’ is duplicated and ‘5’ is missing.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63948b3326086d487e96e4b3
Given a 2D array (i.e., a matrix) containing only 1s (land) and 0s (water), find the biggest island in it. Write a function to return the area of the biggest island.
An island is a connected set of 1s (land) and is surrounded by either an edge or 0s (water). Each cell is considered connected to other cells horizontally or vertically (not diagonally).
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6388d8940cc1849dcbc27fe3
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “listen”, t = “silent”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Example 3:
Input: s = “hello”, t = “world”
Output: false
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63d93a25970f20b6d90eef04
Longest Palindrome
Tech Interview Handbook
https://leetcode.com/problems/longest-palindrome/description/
Longest Palindromic Substring
Tech Interview Handbook
https://leetcode.com/problems/longest-palindromic-substring/description/
Given a string, determine the length of the longest palindrome that can be constructed using the characters from the string. You don’t need to return the palindrome itself, just its maximum possible length.
Examples:
Input: “applepie”
Expected Output: 5
Justification: The longest palindrome that can be constructed from the string is “pepep”, which has a length of 5. There are are other palindromes too but they all will be of length 5.
Input: “aabbcc”
Expected Output: 6
Justification: We can form the palindrome “abccba” using the characters from the string, which has a length of 6.
Input: “bananas”
Expected Output: 5
Justification: The longest palindrome that can be constructed from the string is “anana”, which has a length of 5.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64fd83859592e24ab8b03ab7
Add Binary
Tech Interview Handbook
https://leetcode.com/problems/add-binary/description/
Given an input array of integers nums, find an integer array, let’s call it differenceArray, of the same length as an input integer array.
Each element of differenceArray, i.e., differenceArray[i], should be calculated as follows: take the sum of all elements to the left of index i in array nums (denoted as leftSum[i]), and subtract it from the sum of all elements to the right of index i in array nums (denoted as rightSum[i]), taking the absolute value of the result. Formally:
differenceArray[i] = | leftSum[i] - rightSum[i] |
If there are no elements to the left/right of i, the corresponding sum should be taken as 0.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/651bdef883cd57c6a2fb0ba9
Diameter of a Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/diameter-of-binary-tree/description/
Given two arrays nums1 and nums2 containing integers only, sorted in increasing order, return the smallest integer that appears in both arrays. If there isn’t any integer that exists in both arrays, the function should return -1.
Examples
Example 1:
input: nums1 = [1, 3, 5, 7], nums2 = [3, 4, 5, 6, 8, 10]
expectedOutput: 3
Justification: Both arrays share the integers 3 and 5, but the smallest common integer is 3.
Example 2:
input: nums1 = [2, 4, 6], nums2 = [1, 3, 5]
expectedOutput: -1
Justification: There are no integers common to both nums1 and nums2, hence the output is -1.
Example 3:
input: nums1 = [1, 2, 2, 3], nums2 = [2, 2, 4]
expectedOutput: 2
Justification: The integer 2 is the only common number between nums1 and nums2, appearing multiple times in both, and it is the smallest.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/663c960668f93bfd45e549a1
Given a binary tree, connect each node with its level order successor. The last node of each level should point to a null node.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63989951eb898f72291da2c7
Given the head of a Singly LinkedList, write a method to modify the LinkedList such that the nodes from the second half of the LinkedList are inserted alternately to the nodes from the first half in reverse order. So if the LinkedList has nodes 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null, your method should return 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null.
Your algorithm should use only constant space the input LinkedList should be modified in-place.
Example 1:
Input: 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> null
Output: 2 -> 12 -> 4 -> 10 -> 6 -> 8 -> null
Example 2:
Input: 2 -> 4 -> 6 -> 8 -> 10 -> null
Output: 2 -> 10 -> 4 -> 8 -> 6 -> null
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63923313ae2ec690ac22b61d
Unique Paths
Tech Interview Handbook
https://leetcode.com/problems/unique-paths/description/
LRU Cache
Tech Interview Handbook
https://leetcode.com/problems/lru-cache/description/
Given two strings containing backspaces (identified by the character ‘#’), check if the two strings are equal.
Example 1:
Input: str1=”xy#z”, str2=”xzz#”
Output: true
Explanation: After applying backspaces the strings become “xz” and “xz” respectively.
Example 2:
Input: str1=”xy#z”, str2=”xyz#”
Output: false
Explanation: After applying backspaces the strings become “xz” and “xy” respectively.
Example 3:
Input: str1=”xp#”, str2=”xyz##”
Output: true
Explanation: After applying backspaces the strings become “x” and “x” respectively.
In “xyz##”, the first ‘#’ removes the character ‘z’ and the second ‘#’ removes the character ‘y’.
Example 4:
Input: str1=”xywrrmp”, str2=”xywrrmu#p”
Output: true
Explanation: After applying backspaces the strings become “xywrrmp” and “xywrrmp” respectively.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638fa28f5844e928cbefff88
Determine the depth (or height) of a binary tree, which refers to the number of nodes along the longest path from the root node to the farthest leaf node. If the tree is empty, the depth is 0.
Example:
Input
1
/ \
2 3
/ \
4 5
Output: 3
Explanation: The longest path is 1->2->4 or 1->2->5 with 3 nodes.
Input:
1
\
2
\
3
Expected Output: 3
Justification: There’s only one path 1->2->3 with 3 nodes.
Input:
1
/ \
2 3
/ \
4 7
\
9
Expected Output: 4
Justification: The longest path is 1->2->7->9 with 4 nodes.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64f22febde9e448f5e3f653d
Word Search
Tech Interview Handbook
https://leetcode.com/problems/word-search/description/
Given a binary tree and a number ‘S’, find all paths from root-to-leaf such that the sum of all the node values of each path equals ‘S’.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6399d2d989924acc4bea0939
Given a binary tree, populate an array to represent its zigzag level order traversal. You should populate the values of all nodes of the first level from left to right, then right to left for the next level and keep alternating in the same manner for the following levels.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63979996c8fbc9fe77f1feb0
Lowest Common Ancestor of a Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
Given an undirected graph, represented as a list of edges. Each edge is illustrated as a pair of integers [u, v], signifying that there’s a mutual connection between node u and node v.
Given this graph, a starting node start, and a destination node end, your task is to ascertain if a path exists between the starting node and the destination node.
Examples
Example 1:
Input: 4, [[0,1],[1,2],[2,3]], 0, 3
Expected Output: true
Justification: There’s a path from node 0 -> 1 -> 2 -> 3.
Example 2:
Input: 4, [[0,1],[2,3]], 0, 3
Expected Output: false
Justification: Nodes 0 and 3 are not connected, so no path exists between them.
Example 3:
Input: 5, [[0,1],[3,4]], 0, 4
Expected Output: false
Justification: Nodes 0 and 4 are not connected in any manner.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/651a80b8f0749a59d1e0228d
Time Based Key-Value Store
Tech Interview Handbook
https://leetcode.com/problems/time-based-key-value-store/description/
Evaluate Reverse Polish Notation
Tech Interview Handbook
https://leetcode.com/problems/evaluate-reverse-polish-notation/
Given a binary tree and a number sequence, find if the sequence is present as a root-to-leaf path in the given tree.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6399dbf40d7254be596612d2
Given a stack, sort it using only stack operations (push and pop).
You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The values in the stack are to be sorted in descending order, with the largest elements on top.
Examples
1. Input: [34, 3, 31, 98, 92, 23]
Output: [3, 23, 31, 34, 92, 98]
- Input: [4, 3, 2, 10, 12, 1, 5, 6]
Output: [1, 2, 3, 4, 5, 6, 10, 12] - Input: [20, 10, -5, -1]
Output: [-5, -1, 10, 20]
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64902bd715f14528a3ef7363
Given an array of strings words and two different strings that already exist in the array word1 and word2, return the shortest distance between these two words in the list.
Example 1:
Input: words = [“the”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog”], word1 = “fox”, word2 = “dog”
Output: 5
Explanation: The distance between “fox” and “dog” is 5 words.
Example 2:
Input: words = [“a”, “c”, “d”, “b”, “a”], word1 = “a”, word2 = “b”
Output: 1
Explanation: The shortest distance between “a” and “b” is 1 word. Please note that “a” appeared twice.
Example 3:
Input: words = [“a”, “b”, “c”, “d”, “e”], word1 = “a”, word2 = “e”
Output: 4
Explanation: The distance between “a” and “e” is 4 words.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63daaa1a0d01fe363b68c8d4
Given a root node of the Binary Search Tree (BST) and integer ‘k’. Return the Kth smallest element among all node values of the binary tree.
Examples:
Example 1:
Input:
8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 k = 4 Expected Output: 6 Justification: The in-order traversal of the tree is [1, 3, 4, 6, 7, 8, 10, 13, 14]. The 4th element in this sequence is 6.
Example 2:
Input:
5 / \ 2 6 / 1 k = 3 Expected Output: 5 Justification: The in-order traversal of the tree is [1, 2, 5, 6]. The 3rd element in this sequence is 5.
Example 3:
Input:
1
\
3
/
2
k = 2
Expected Output: 2
Justification: The in-order traversal of the tree is [1, 2, 3]. The 2nd element in this sequence is 2.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64f2c4a66a55d05df40e019d
Lowest Common Ancestor of a Binary Search Tree
Tech Interview Handbook
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
We are given an array containing n objects. Each object, when created, was assigned a unique number from the range 1 to n based on their creation sequence. This means that the object with sequence number 3 was created just before the object with sequence number 4.
Write a function to sort the objects in-place on their creation sequence number in without using any extra space. For simplicity, let’s assume we are passed an integer array containing only the sequence numbers, though each number is actually an object.
Example 1:
Input: [3, 1, 5, 4, 2]
Output: [1, 2, 3, 4, 5]
Example 2:
Input: [2, 6, 4, 3, 1, 5]
Output: [1, 2, 3, 4, 5, 6]
Example 3:
Input: [1, 5, 6, 4, 3, 2]
Output: [1, 2, 3, 4, 5, 6]
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6393a98bd8a93f4bff961b4d
Given a binary tree, connect each node with its level order successor. The last node of each level should point to the first node of the next level.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63989c048d812da0aa12bff7
Implement Trie (Prefix Tree)
Tech Interview Handbook
https://leetcode.com/problems/implement-trie-prefix-tree/description/
Climbing Stairs
Tech Interview Handbook
https://leetcode.com/problems/climbing-stairs/description/
Given divisor1, divisor2, uniqueCnt1, and uniqueCnt2 integers, find the smallest possible maximum integer that could be present in either array after they are filled according to the below conditions.
You can take two arrays arr1 and arr2 which are initially empty.
arr1 contains total uniqueCnt1 different positive integers, each of them is not divisible by divisor1.
arr2 contains total uniqueCnt2 different positive integers, each of them is divisible by divisor2.
There are no common integers in both arrays.
Examples
Example 1:
Input: uniqueCnt1 = 2, divisor1 = 2, uniqueCnt2 = 2, divisor2 = 3
Expected Output: 4
Explanation: The optimal arrays could be arr1 = [1, 3] (numbers not divisible by 2) and arr2 = [2, 4] (numbers not divisible by 3). The maximum number among both arrays is 4.
Example 2:
Input: uniqueCnt1 = 3, divisor1 = 3, uniqueCnt2 = 4, divisor2 = 4
Expected Output: 7
Explanation: Possible arrays are arr1 = [1, 2, 4] and arr2 = [3, 5, 6, 7]. The highest integer used is 7.
Example 3:
Input: uniqueCnt1 = 1, divisor1 = 7, uniqueCnt2 = 1, divisor2 = 10
Expected Output: 2
Explanation: We can use arr1 = [1] (since it’s not divisible by 7) and arr2 = [2] (since it’s not divisible by 10). The highest integer here is 2.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/663cbbbd0f27fd5b0be90e83
We are given an array containing n distinct numbers taken from the range 0 to n. Since the array has only n numbers out of the total n+1 numbers, find the missing number.
Example 1:
Input: [4, 0, 3, 1]
Output: 2
Example 2:
Input: [8, 3, 5, 2, 4, 6, 0, 1]
Output: 7
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6393ab5cd8a93f4bff961bc7
Container with Most Water
Tech Interview Handbook
https://leetcode.com/problems/container-with-most-water/description/
Find All Anagrams in a String
Tech Interview Handbook
https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
Given a binary tree, populate an array to represent its level-by-level traversal in reverse order, i.e., the lowest level comes first. You should populate the values of all nodes in each level from left to right in separate sub-arrays.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6397900a721fdbdbc77e92ad
Permutations
Tech Interview Handbook
https://leetcode.com/problems/permutations/description/
Flood Fill
Tech Interview Handbook
https://leetcode.com/problems/flood-fill/description/
Given two lists of intervals, find the intersection of these two lists. Each list consists of disjoint intervals sorted on their start time.
Example 1:
Input: arr1=[[1, 3], [5, 6], [7, 9]], arr2=[[2, 3], [5, 7]]
Output: [2, 3], [5, 6], [7, 7]
Explanation: The output list contains the common intervals between the two lists.
Example 2:
Input: arr1=[[1, 3], [5, 7], [9, 12]], arr2=[[5, 10]]
Output: [5, 7], [9, 10]
Explanation: The output list contains the common intervals between the two lists.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/639249470cddc1617da1b889
We are given an unsorted array containing numbers taken from the range 1 to ‘n’. The array can have duplicates, which means some numbers will be missing. Find all those missing numbers.
Example 1:
Input: [2, 3, 1, 8, 2, 3, 5, 1]
Output: 4, 6, 7
Explanation: The array should have all numbers from 1 to 8, due to duplicates 4, 6, and 7 are missing.
Example 2:
Input: [2, 4, 1, 2]
Output: 3
Example 3:
Input: [2, 3, 2, 1]
Output: 4
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6393ada834689e585e94a1b9
Invert Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/invert-binary-tree/description/
Given an absolute file path in a Unix-style file system, simplify it by converting “..” to the previous directory and removing any “.” or multiple slashes. The resulting string should represent the shortest absolute path.
Examples:
1.
Input: “/a//b////c/d//././/..”
Output: “/a/b/c”
- Input: “/../”
Output: “/” - Input: “/home//foo/”
Output: “/home/foo”
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64902fec5d0034e2d16110aa
Given an array of sorted numbers, move all non-duplicate number instances at the beginning of the array in-place. The non-duplicate numbers should be sorted and you should not use any extra space so that the solution has constant space complexity i.e., .
Move all the unique number instances at the beginning of the array and after moving return the length of the subarray that has no duplicate in it.
Example 1:
Input: [2, 3, 3, 3, 6, 9, 9]
Output: 4
Explanation: The first four elements after moving element will be [2, 3, 6, 9].
Example 2:
Input: [2, 2, 2, 11]
Output: 2
Explanation: The first two elements after moving elements will be [2, 11].
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638e33feac0cc8a9358a25ac
Combination Sum
Tech Interview Handbook
https://leetcode.com/problems/combination-sum/description/
Given a string, determine the maximum number of times the word “balloon” can be formed using the characters from the string. Each character in the string can be used only once.
Examples:
Example 1:
Input: “balloonballoon”
Expected Output: 2
Justification: The word “balloon” can be formed twice from the given string.
Example 2:
Input: “bbaall”
Expected Output: 0
Justification: The word “balloon” cannot be formed from the given string as we are missing the character ‘o’ twice.
Example 3:
Input: “balloonballoooon”
Expected Output: 2
Justification: The word “balloon” can be formed twice, even though there are extra ‘o’ characters.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64fd60b82322f34a81703611
First Bad Version
Tech Interview Handbook
https://leetcode.com/problems/first-bad-version/description/
Given a binary search tree (BST) and a target number, you need to find a node value in the BST that is closest to the given target. A BST is a tree where for every node, the values in the left subtree are smaller than the node, and the values in the right subtree are greater.
Examples:
Example 1:
Input:
Tree: 5 / \ 3 8 / \ / \ 1 4 6 9 Target: 6.4
Expected Output: 6
Justification: The values 6 and 8 are the closest numbers to 6.4 in the tree. Among them, 6 is closer.
Example 2:
Input:
Tree: 20 / \ 10 30 Target: 25
Expected Output: 20
Justification: 20 and 30 are the closest numbers to 25. However, 20 is closer than 30.
Example 3:
Input:
Tree: 2 / \ 1 3 Target: 2.9
Expected Output: 3
Justification: 3 is the closest value to 2.9 in the tree.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64f2ccca6a55d05df40e51b2
Given a binary tree and a number ‘S’, find all paths in the tree such that the sum of all the node values of each path equals ‘S’. Please note that the paths can start or end at any node but all paths must follow direction from parent to child (top to bottom).
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6399e360a0e842698079404f
Accounts Merge
Tech Interview Handbook
https://leetcode.com/problems/accounts-merge/description/
Valid Anagram
Tech Interview Handbook
https://leetcode.com/problems/valid-anagram/description/
A bike rider is going on a ride. The road contains n + 1 points at different altitudes. The rider starts from point 0 at an altitude of 0.
Given an array of integers gain of length n, where gain[i] represents the net gain in altitude between points i and i + 1 for all (0 <= i < n), return the highest altitude of a point.
Examples
Example 1
Input: gain = [-5, 1, 5, 0, -7]
Expected Output: 1
Justification: The altitude changes are [-5, -4, 1, 1, -6], where 1 is the highest altitude reached.
Example 2
Input: gain = [4, -3, 2, -1, -2]
Expected Output: 4
Justification: The altitude changes are [4, 1, 3, 2, 0], where 4 is the highest altitude reached.
Example 3
Input: gain = [2, 2, -3, -1, 2, 1, -5]
Expected Output: 4
Justification: The altitude changes are [2, 4, 1, 0, 2, 3, -2], where 4 is the highest altitude reached.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/651be1c083cd57c6a2fb1542
Find the minimum depth of a binary tree. The minimum depth is the number of nodes along the shortest path from the root node to the nearest leaf node.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6397ac3f197fbea348eb0f04
Given an array containing 0s, 1s and 2s, sort the array in-place. You should treat numbers of the array as objects, hence, we can’t count 0s, 1s, and 2s to recreate the array.
The flag of the Netherlands consists of three colors: red, white and blue; and since our input array also consists of three different numbers that is why it is called Dutch National Flag problem.
Example 1:
Input: [1, 0, 2, 1, 0]
Output: [0 0 1 1 2]
Example 2:
Input: [2, 2, 0, 1, 2, 0]
Output: [0 0 1 2 2 2 ]
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638f9b3e239cfbfb10ee82f3
Given a binary matrix that has dimensions , consisting of ones and zeros, determine the row that contains the highest number of ones and return two values: the zero-based index of this row and the actual count of ones it possesses.
If there is a tie, i.e., multiple rows contain the same maximum number of ones, we must select the row with the lowest index.
Examples
Example 1:
Input: [[1, 0], [1, 1], [0, 1]]
Expected Output: [1, 2]
Justification: The second row [1, 1] contains the most ones, so the output is [1, 2].
Example 2:
Input: [[0, 1, 1], [0, 1, 1], [1, 1, 1]]
Expected Output: [2, 3]
Justification: The third row [1, 1, 1] has the most ones, leading to the output [2, 3].
Example 3:
Input: [[1, 0, 1], [0, 0, 1], [1, 1, 0]]
Expected Output: [0, 2]
Justification: Both the first and third rows contain two ones, but we choose the first due to its lower index, resulting in [0, 2].
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/651bbf379aa5328fe5ae4983
Task Scheduler
Tech Interview Handbook
https://leetcode.com/problems/task-scheduler/description/
HARD
We are given an array containing positive and negative numbers. Suppose the array contains a number ‘M’ at a particular index. Now, if ‘M’ is positive we will move forward ‘M’ indices and if ‘M’ is negative move backwards ‘M’ indices. You should assume that the array is circular which means two things:
If, while moving forward, we reach the end of the array, we will jump to the first element to continue the movement.
If, while moving backward, we reach the beginning of the array, we will jump to the last element to continue the movement.
Write a method to determine if the array has a cycle. The cycle should have more than one element and should follow one direction which means the cycle should not contain both forward and backward movements.
Example 1:
Input: [1, 2, -1, 2, 2]
Output: true
Explanation: The array has a cycle among indices: 0 -> 1 -> 3 -> 0
Example 2:
Input: [2, 2, -1, 2]
Output: true
Explanation: The array has a cycle among indices: 1 -> 3 -> 1
Example 3:
Input: [2, 1, -1, -2]
Output: false
Explanation: The array does not have any cycle.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6392394ee4cb724ea7189954
Given two binary trees, root1 and root2, merge them into a single, new binary tree.
If two nodes from the given trees share the same position, their values should be summed up in the resulting tree. If a node exists in one tree but not in the other, the resulting tree should have a node at the same position with the value from the existing node.
Examples
Example 1:
Trees:
Tree 1: 1 Tree 2: 1
/ \ / \
3 2 2 3
Merged: 2
/ \
5 5
Justification: The root nodes of both trees have the value 1, so the merged tree’s root has a value of 1 + 1 = 2. For the left child, 3 + 2 = 5 and for the right child, 2 + 3 = 5.
Example 2:
Trees:
Tree 1: 5 Tree 2: 3
/ \ / \
4 7 2 6
Merged: 8
/ \
6 13
Justification: The root nodes have values 5 and 3 respectively. So, the merged tree’s root value becomes 5 + 3 = 8. The left child is 4 + 2 = 6 and the right child is 7 + 6 = 13.
Example 3:
Trees:
Tree 1: 2 Tree 2: 2
\ /
5 3
Merged: 4
/ \
3 5
Justification: The root nodes have values 2 each, so the merged tree’s root value is 2 + 2 = 4. Tree 1 doesn’t have a left child, so we take the left child of Tree 2 as it is, which is 3. Similarly, Tree 2 doesn’t have a right child, so the merged tree’s right child is the same as Tree 1, which is 5.
Example 4:
Trees:
Tree 1: 10 Tree 2: 10
/ \ / \
5 15 6 16
Merged: 20
/ \
11 31
Justification: The root nodes have the value 10, so they add up to 20 for the merged tree. The left child values add up to 5 + 6 = 11 and the right child values sum up to 15 + 16 = 31.
https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64f2d12d9654890c5a9ab5ef
Validate Binary Search Tree
Tech Interview Handbook
https://leetcode.com/problems/validate-binary-search-tree/description/
Best Time to Buy and Sell Stock
Tech Interview Handbook
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
We are given an unsorted array containing n+1 numbers taken from the range 1 to n. The array has only one duplicate but it can be repeated multiple times. Find that duplicate number without using any extra space. You are, however, allowed to modify the input array.
Example 1:
Input: [1, 4, 4, 3, 2]
Output: 4
Example 2:
Input: [2, 1, 3, 3, 5, 4]
Output: 3
Example 3:
Input: [2, 4, 1, 4, 4]
Output: 4
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6393aee5ba7c985340679287
Given a list of intervals, merge all the overlapping intervals to produce a list that has only mutually exclusive intervals.
Example 1:
Intervals: [[1,4], [2,5], [7,9]]
Output: [[1,5], [7,9]]
Explanation: Since the first two intervals [1,4] and [2,5] overlap, we merged them into one [1,5].
Example 2:
Intervals: [[6,7], [2,4], [5,9]]
Output: [[2,4], [5,9]]
Explanation: Since the intervals [6,7] and [5,9] overlap, we merged them into one [5,9].
Example 3:
Intervals: [[1,4], [2,6], [3,5]]
Output: [[1,6]]
Explanation: Since all the given intervals overlap, we merged them into one.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63923e6de4cb724ea719007a
Valid Parentheses
Tech Interview Handbook
https://leetcode.com/problems/valid-parentheses/description/
Given the head of a Singly LinkedList, write a function to determine if the LinkedList has a cycle in it or not.
https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/639105492e1c0a1cfe6c1eb9