Practice Problems Flashcards

1
Q

01 Matrix
Tech Interview Handbook
https://leetcode.com/problems/01-matrix/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Longest Palindrome
Tech Interview Handbook
https://leetcode.com/problems/longest-palindrome/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Longest Palindromic Substring
Tech Interview Handbook
https://leetcode.com/problems/longest-palindromic-substring/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Add Binary
Tech Interview Handbook
https://leetcode.com/problems/add-binary/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Diameter of a Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/diameter-of-binary-tree/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Unique Paths
Tech Interview Handbook
https://leetcode.com/problems/unique-paths/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

LRU Cache
Tech Interview Handbook
https://leetcode.com/problems/lru-cache/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Word Search
Tech Interview Handbook
https://leetcode.com/problems/word-search/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Lowest Common Ancestor of a Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Time Based Key-Value Store
Tech Interview Handbook
https://leetcode.com/problems/time-based-key-value-store/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Evaluate Reverse Polish Notation
Tech Interview Handbook
https://leetcode.com/problems/evaluate-reverse-polish-notation/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

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]

  1. Input: [4, 3, 2, 10, 12, 1, 5, 6]
    Output: [1, 2, 3, 4, 5, 6, 10, 12]
  2. Input: [20, 10, -5, -1]
    Output: [-5, -1, 10, 20]

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64902bd715f14528a3ef7363

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Lowest Common Ancestor of a Binary Search Tree
Tech Interview Handbook
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Implement Trie (Prefix Tree)
Tech Interview Handbook
https://leetcode.com/problems/implement-trie-prefix-tree/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Climbing Stairs
Tech Interview Handbook
https://leetcode.com/problems/climbing-stairs/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Container with Most Water
Tech Interview Handbook
https://leetcode.com/problems/container-with-most-water/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Find All Anagrams in a String
Tech Interview Handbook
https://leetcode.com/problems/find-all-anagrams-in-a-string/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Permutations
Tech Interview Handbook
https://leetcode.com/problems/permutations/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Flood Fill
Tech Interview Handbook
https://leetcode.com/problems/flood-fill/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Invert Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/invert-binary-tree/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

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”

  1. Input: “/../”
    Output: “/”
  2. Input: “/home//foo/”
    Output: “/home/foo”

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64902fec5d0034e2d16110aa

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Combination Sum
Tech Interview Handbook
https://leetcode.com/problems/combination-sum/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

First Bad Version
Tech Interview Handbook
https://leetcode.com/problems/first-bad-version/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

Accounts Merge
Tech Interview Handbook
https://leetcode.com/problems/accounts-merge/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

Valid Anagram
Tech Interview Handbook
https://leetcode.com/problems/valid-anagram/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Task Scheduler
Tech Interview Handbook
https://leetcode.com/problems/task-scheduler/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

Validate Binary Search Tree
Tech Interview Handbook
https://leetcode.com/problems/validate-binary-search-tree/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

Best Time to Buy and Sell Stock
Tech Interview Handbook
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

Valid Parentheses
Tech Interview Handbook
https://leetcode.com/problems/valid-parentheses/description/

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

Rotting Oranges
Tech Interview Handbook
https://leetcode.com/problems/rotting-oranges/description/

A
70
Q

Given a list of integers, determine the count of numbers for which there exists another number in the list that is greater by exactly one unit.

In other words, for each number x in the list, if x + 1 also exists in the list, then x is considered for the count.

Examples:

Example 1:

Input: [4, 3, 1, 5, 6]
Expected Output: 3
Justification: The numbers 4, 3, and 5 have 5, 4, and 6 respectively in the list, which are greater by exactly one unit.
Example 2:

Input: [7, 8, 9, 10]
Expected Output: 3
Justification: The numbers 7, 8, and 9 have 8, 9, and 10 respectively in the list, which are greater by exactly one unit.
Example 3:

Input: [11, 13, 15, 16]
Expected Output: 1
Justification: Only the number 15 has 16 in the list, which is greater by exactly one unit.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64fd596617ec3ee5197634f4

A
71
Q

Given an array of integers nums, return the number of good pairs.

A pair (i, j) is called good if nums[i] == nums[j] and i < j.

Example 1:

Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good pairs, here are the indices: (0,3), (0,4), (3,4), (2,5).
Example 2:

Input: nums = [1,1,1,1]
Output: 6
Explanation: Each pair in the array is a ‘good pair’.
Example 3:

Input: words = nums = [1,2,3]
Output: 0
Explanation: No number is repeating.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63dac2b3bbc58b880202c251

A
72
Q

Given a string, write a function that uses a stack to reverse the string. The function should return the reversed string.

Examples
Example 1:

Input: “Hello, World!”
Output: “!dlroW ,olleH”
Example 2:

Input: “OpenAI”
Output: “IAnepO”
Example 3:

Input: “Stacks are fun!”
Output: “!nuf era skcatS”

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6490189d822978cc90e2ace0

A
73
Q

Clone Graph
Tech Interview Handbook
https://leetcode.com/problems/clone-graph/description/

A
74
Q

Given a doubly linked list, determine whether it is a palindrome.

A doubly linked list is a palindrome if it reads the same backward as forward, utilizing the previous and next pointers of the nodes.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/65101ae0919cfcaef3156b95

A
75
Q

Given a binary tree and a number ‘S’, find if the tree has a path from root-to-leaf such that the sum of all the node values of that path equals ‘S’.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6398a0a974c44bdc88129966

A
76
Q

Number of Islands
Tech Interview Handbook
https://leetcode.com/problems/number-of-islands/description/

A
77
Q

Given a one-dimensional array of integers, create a new array that represents the running sum of the original array.

The running sum at position i in the new array is calculated as the sum of all the numbers in the original array from the 0th index up to the i-th index (inclusive). Formally, the resulting array should be computed as follows: result[i] = sum(nums[0] + nums[1] + … + nums[i]) for each i from 0 to the length of the array minus one.

Examples
Example 1

Input: [2, 3, 5, 1, 6]
Expected Output: [2, 5, 10, 11, 17]
Justification:
For i=0: 2
For i=1: 2 + 3 = 5
For i=2: 2 + 3 + 5 = 10
For i=3: 2 + 3 + 5 + 1 = 11
For i=4: 2 + 3 + 5 + 1 + 6 = 17
Example 2

Input: [1, 1, 1, 1, 1]
Expected Output: [1, 2, 3, 4, 5]
Justification: Each element is simply the sum of all preceding elements plus the current element.
Example 3

Input: [-1, 2, -3, 4, -5]
Expected Output: [-1, 1, -2, 2, -3]
Justification: Negative numbers are also summed up in the same manner as positive ones.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/651bdcbb5a884f25a9c00863

A
78
Q

Balanced Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/balanced-binary-tree/description/

A
79
Q

Kth Smallest Element in a BST
Tech Interview Handbook
https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/

A
80
Q

Search in Rotated Sorted Array
Tech Interview Handbook
https://leetcode.com/problems/search-in-rotated-sorted-array/description/

A
81
Q

Given a positive integer n, write a function that returns its binary equivalent as a string. The function should not use any in-built binary conversion function.

Examples
Example 1:

Input: 2
Output: “10”
Explanation: The binary equivalent of 2 is 10.
Example 2:

Input: 7
Output: “111”
Explanation: The binary equivalent of 7 is 111.
Example 3:

Input: 18
Output: “10010”
Explanation: The binary equivalent of 18 is 10010.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6490145d2d3ff689ba978d02

A
82
Q

Course Schedule
Tech Interview Handbook
https://leetcode.com/problems/course-schedule/description/

A
83
Q

You are given an m x n matrix accounts where accounts[i][j] is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank.

Return the wealth that the richest customer has.

Imagine every customer has multiple bank accounts, with each account holding a certain amount of money. The total wealth of a customer is calculated by summing all the money across all their multiple.

Examples
Example 1:

Input: [[5,2,3],[0,6,7]]
Expected Output: 13
Justification: The total wealth of the first customer is 10 and of the second customer is 13. So, the output is 13 as it’s the maximum among all customers.
Example 2:

Input: [[1,2],[3,4],[5,6]]
Expected Output: 11
Justification: Total wealth for each customer is [3, 7, 11]. Maximum of these is 11.
Example 3:

Input: [[10,100],[200,20],[30,300]]
Expected Output: 330
Justification: Total wealth for each customer is [110, 220, 330]. The wealthiest customer has 330.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/651bba625b23f17e05299274

A
84
Q

Given an array of unsorted numbers and a target number, find all unique quadruplets in it, whose sum is equal to the target number.

Example 1:

Input: [4, 1, 2, -1, 1, -3], target=1
Output: [-3, -1, 1, 4], [-3, 1, 1, 2]
Explanation: Both the quadruplets add up to the target.
Example 2:

Input: [2, 0, -1, 1, -2, 2], target=2
Output: [-2, 0, 2, 2], [-1, 0, 1, 2]
Explanation: Both the quadruplets add up to the target.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638f9cab70b3327d7a210d9d

A
85
Q

You are given a 2D matrix containing only 1s (land) and 0s (water).

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

Two islands are considered the same if and only if they can be translated (not rotated or reflected) to equal each other.

Write a function to find the number of distinct islands in the given matrix.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638c920fa19edaace544c805

A
86
Q

Given two strings. The first string represents types of jewels, where each character is a unique type of jewel. The second string represents stones you have, where each character represents a type of stone. Determine how many of the stones you have are also jewels.

Examples:

Example 1:

Input: Jewels = “abc”, Stones = “aabbcc”
Expected Output: 6
Justification: All the stones are jewels.
Example 2:

Input: Jewels = “aA”, Stones = “aAaZz”
Expected Output: 3
Justification: There are 2 ‘a’ and 1 ‘A’ stones that are jewels.
Example 3:

Input: Jewels = “zZ”, Stones = “zZzZzZ”
Expected Output: 6
Justification: All the stones are jewels.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64fd6e571dcab5beab2127d1

A
87
Q

Middle of the Linked List
Tech Interview Handbook
https://leetcode.com/problems/middle-of-the-linked-list/description/

A
88
Q

Given a binary tree, find the length of its diameter. The diameter of a tree is the number of nodes on the longest path between any two leaf nodes. The diameter of a tree may or may not pass through the root.

Note: You can always assume that there are at least two leaf nodes in the given tree.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/639b62040dbaa5118a4b5e96

A
89
Q

Binary Tree Level Order Traversal
Tech Interview Handbook
https://leetcode.com/problems/binary-tree-level-order-traversal/description/

A
90
Q

You are given a 2D matrix containing only 1s (land) and 0s (water).

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

There are no lakes on the island, so the water inside the island is not connected to the water around it. A cell is a square with a side length of 1..

The given matrix has only one island, write a function to find the perimeter of that island.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638c857a34bb69a286c79f30

A
91
Q

You are given an nums array containing n integers and an integer k. In a single operation, you can choose any index i and increment the nums[i] by 1.

Return the maximum possible frequency of any element of nums after performing at most k operations.

Examples
Example 1:

Input: nums = [1, 2, 3], k = 3
Expected Output: 3
Explanation: We can increment the number 1 two times and 2 one time. The final array will be [3, 3, 3]. Now, the number 3 appears 3 times in the array [3, 3, 3].
Example 2:

Input: nums = [4, 4, 4, 1], k = 2
Expected Output: 3
Explanation: We can increment the number 1 two times (1 -> 2 -> 3). Now, the number 4 still appears 3 times, which is the maximum frequency that can be achieved with 2 operations.
Example 3:

Input: nums = [10, 9, 9, 4, 8], k = 5
Expected Output: 4
Explanation: We can increment the number 9 one time and the number 8 two times (9 -> 10, 9 -> 10; 8 -> 9 -> 10). The number 10 will then appear 4 times in the array [10, 10, 10, 4, 10].

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/663caa3b68f93bfd45e64bbe

A
92
Q

Given the head of a Singly LinkedList, write a method to return the middle node of the LinkedList.

If the total number of nodes in the LinkedList is even, return the second middle node.

Example 1:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> null
Output: 3
Example 2:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Output: 4
Example 3:

Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
Output: 4

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63922bb1b689b698075a9a59

A
93
Q

Given the head of a Singly LinkedList that contains a cycle, write a function to find the starting node of the cycle.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6391094ea637a0a5c98582ee

A
94
Q

Given an array of unsorted numbers and a target number, find a triplet in the array whose sum is as close to the target number as possible, return the sum of the triplet. If there are more than one such triplet, return the sum of the triplet with the smallest sum.

Example 1:

Input: [-1, 0, 2, 3], target=3
Output: 2
Explanation: The triplet [-1, 0, 3] has the sum ‘2’ which is closest to the target.

There are two triplets with distance ‘1’ from the target: [-1, 0, 3] & [-1, 2, 3]. Between these two triplets, the correct answer will be [-1, 0, 3] as it has a sum ‘2’ which is less than the sum of the other triplet which is ‘4’. This is because of the following requirement: ‘If there are more than one such triplet, return the sum of the triplet with the smallest sum.’
Example 2:

Input: [-3, -1, 1, 2], target=1
Output: 0
Explanation: The triplet [-3, 1, 2] has the closest sum to the target.
Example 3:

Input: [1, 0, 1, 1], target=100
Output: 3
Explanation: The triplet [1, 1, 1] has the closest sum to the target.
Example 4:

Input: [0, 0, 1, 1, 2, 6], target=5
Output: 4
Explanation: There are two triplets with distance ‘1’ from target: [1, 1, 2] & [0, 0, 6]. Between these two triplets, the correct answer will be [1, 1, 2] as it has a sum ‘4’ which is less than the sum of the other triplet which is ‘6’. This is because of the following requirement: ‘If there are more than one such triplet, return the sum of the triplet with the smallest sum.’

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638f7323ae53511bdc364dab

A
95
Q

Binary Tree Right Side View
Tech Interview Handbook
https://leetcode.com/problems/binary-tree-right-side-view/description/

A
96
Q

Implement Queue using Stacks
Tech Interview Handbook
https://leetcode.com/problems/implement-queue-using-stacks/description/

A
97
Q

Given a Binary Search Tree (BST) and a range defined by two integers, L and R, calculate the sum of all the values of nodes that fall within this range. The node’s value is inclusive within the range if and only if L <= node’s value <= R.

Examples:

Example 1:

Input:

Tree:
10
/ \
5 15
/ \ \
3 7 18
Range: [7, 15]
Expected Output: 32

Justification: The values that fall within the range [7, 15] are 7, 10, and 15. Their sum is 7 + 10 + 15 = 32.

Example 2:

Input:

Tree:
20
/ \
5 25
/ \
3 10
Range: [3, 10]
Expected Output: 18

Justification: The values within the range [3, 10] are 3, 5, and 10. Their sum is 3 + 5 + 10 = 18.

Example 3:

Input:

Tree:
30
\
35
/
32
Range: [30, 34]
Expected Output: 62

Justification: The values within the range [30, 34] are 30 and 32. Their sum is 30 + 32 = 62.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64f2c93d6a55d05df40e28c2

A
98
Q

Given an array, find the length of the smallest subarray in it which when sorted will sort the whole array.

Example 1:

Input: [1, 2, 5, 3, 7, 10, 9, 12]
Output: 5
Explanation: We need to sort only the subarray [5, 3, 7, 10, 9] to make the whole array sorted
Example 2:

Input: [1, 3, 2, 0, -1, 7, 10]
Output: 5
Explanation: We need to sort only the subarray [1, 3, 2, 0, -1] to make the whole array sorted
Example 3:

Input: [1, 2, 3]
Output: 0
Explanation: The array is already sorted
Example 4:

Input: [3, 2, 1]
Output: 3
Explanation: The whole array needs to be sorted.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638fa5205844e928cbf004bf

A
99
Q

Given a string s, where * represents a star. We can remove a star along with its closest non-star character to its left in a single operation.

The task is to perform as many such operations as possible until all stars have been removed and return the resultant string.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64903dd726f49aa43e0b32cb

A
100
Q

Given a binary tree, populate an array to represent the averages of all of its levels.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6397aabd009f4ccc0648b8ca

A
101
Q

Longest Substring w/o Repeating Characters
Tech Interview Handbook
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

A
102
Q

Given a binary tree where each node can only have a digit (0-9) value, each root-to-leaf path will represent a number. Find the total sum of all the numbers represented by all paths.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6399d8d20d7254be596610f4

A
103
Q

Given two strings, one representing a ransom note and the other representing the available letters from a magazine, determine if it’s possible to construct the ransom note using only the letters from the magazine. Each letter from the magazine can be used only once.

Examples:

Example 1:

Input: Ransom Note = “hello”, Magazine = “hellworld”
Expected Output: true
Justification: The word “hello” can be constructed from the letters in “hellworld”.
Example 2:

Input: Ransom Note = “notes”, Magazine = “stoned”
Expected Output: true
Justification: The word “notes” can be fully constructed from “stoned” from its first 5 letters.
Example 3:

Input: Ransom Note = “apple”, Magazine = “pale”
Expected Output: false
Justification: The word “apple” cannot be constructed from “pale” as we are missing one ‘p’.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64fd6a77826e0e8c47b7f345

A
104
Q

Given the head of a Singly LinkedList, write a method to check if the LinkedList is a palindrome or not.

Your algorithm should use constant space and the input LinkedList should be in the original form once the algorithm is finished. The algorithm should have O(N) time complexity where ‘N’ is the number of nodes in the LinkedList.

Example 1:

Input: 2 -> 4 -> 6 -> 4 -> 2 -> null
Output: true
Example 2:

Input: 2 -> 4 -> 6 -> 4 -> 2 -> 2 -> null
Output: false

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63922d27b689b698075a9ab3

A
105
Q

Contains Duplicate
Tech Interview Handbook
https://leetcode.com/problems/contains-duplicate/description/

A
106
Q

Linked List Cycle
Tech Interview Handbook
https://leetcode.com/problems/linked-list-cycle/description/

A
107
Q

Given an array of integers, determine if the number of times each distinct integer appears in the array is unique.

In other words, the occurrences of each integer in the array should be distinct from the occurrences of every other integer.

Examples:

Input: [4, 5, 4, 6, 6, 6]
Expected Output: true
Justification: The number 4 appears 2 times, 5 appears 1 time, and 6 appears 3 times. All these occurrences (1, 2, 3) are unique.
Input: [7, 8, 8, 9, 9, 9, 10, 10]
Expected Output: false
Justification: The number 7 appears 1 time, 8 appears 2 times, 9 appears 3 times, and 10 appears 2 times. The occurrences are not unique since the number 2 appears twice.
Input: [11, 12, 13, 14, 14, 15, 15, 15]
Expected Output: false
Justification: The number 11 appears 1 time, 12 appears 1 time, 13 appears 1 time, 14 appears 2 times, and 15 appears 3 times. These occurrences are not unique.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64fd7aeb75e03702eeb2c23e

A
108
Q

String to Integer (atoi)
Tech Interview Handbook
https://leetcode.com/problems/string-to-integer-atoi/description/

A
109
Q

Subsets
Tech Interview Handbook
https://leetcode.com/problems/subsets/description/

A
110
Q

Reverse Linked List
Tech Interview Handbook
https://leetcode.com/problems/majority-element/description/

A
111
Q

Given an array of positive integers and a number ‘S,’ find the length of the smallest contiguous subarray whose sum is greater than or equal to ‘S’. Return 0 if no such subarray exists.

Example 1:

Input: [2, 1, 5, 2, 3, 2], S=7
Output: 2
Explanation: The smallest subarray with a sum greater than or equal to ‘7’ is [5, 2].
Example 2:

Input: [2, 1, 5, 2, 8], S=7
Output: 1
Explanation: The smallest subarray with a sum greater than or equal to ‘7’ is [8].
Example 3:

Input: [3, 4, 1, 1, 6], S=8
Output: 3
Explanation: Smallest subarrays with a sum greater than or equal to ‘8’ are [3, 4, 1] or [1, 1, 6].

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/636b1d083b22faa3e89b2478

A
112
Q

Given a string s containing (, ), [, ], {, and } characters. Determine if a given string of parentheses is balanced.

A string of parentheses is considered balanced if every opening parenthesis has a corresponding closing parenthesis in the correct order.

Example 1:

Input: String s = “{[()]}”;
Expected Output: true
Explanation: The parentheses in this string are perfectly balanced. Every opening parenthesis ‘{‘, ‘[’, ‘(‘ has a corresponding closing parenthesis ‘}’, ‘]’, ‘)’ in the correct order.

Example 2:

Input: string s = “{[}]”;
Expected Output: false
Explanation: The brackets are not balanced in this string. Although it contains the same number of opening and closing brackets for each type, they are not correctly ordered. The ‘]’ closes ‘[’ before ‘{‘ can be closed by ‘}’, and similarly, ‘}’ closes ‘{‘ before ‘[’ can be closed by ‘]’.

Example 3:

Input: String s = “(]”;
Expected Output: false
Explanation: The parentheses in this string are not balanced. Here, ‘)’ does not have a matching opening parenthesis ‘(‘, and similarly, ‘]’ does not have a matching opening bracket ‘[’.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64804bc840c761a52b2a872f

A
113
Q

Given an array of intervals representing ‘N’ appointments, find out if a person can attend all the appointments.

Example 1:

Appointments: [[1,4], [2,5], [7,9]]
Output: false
Explanation: Since [1,4] and [2,5] overlap, a person cannot attend both of these appointments.
Example 2:

Appointments: [[6,7], [2,4], [8,12]]
Output: true
Explanation: None of the appointments overlap, therefore a person can attend all of them.
Example 3:

Appointments: [[4,5], [2,3], [3,6]]
Output: false
Explanation: Since [4,5] and [3,6] overlap, a person cannot attend both of these appointments.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/639363de4599b3e5714ab022

A
114
Q

Construct Binary Tree from Preorder and Inorder Traversal
Tech Interview Handbook
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

A
115
Q

Given an array, print the Next Greater Element (NGE) for every element.

The Next Greater Element for an element x is the first greater element on the right side of x in the array.

Elements for which no greater element exist, consider the next greater element as -1.

Examples
Example 1:

Input: [4, 5, 2, 25]
Output: [5, 25, 25, -1]
Example 1:

Input: [13, 7, 6, 12]
Output: [-1, 12, 12, -1]
Example 1:

Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, -1]

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/652377d9b28abf3c6db3b596

A
116
Q

Given a list of non-overlapping intervals sorted by their start time, insert a given interval at the correct position and merge all necessary intervals to produce a list that has only mutually exclusive intervals.

Example 1:

Input: Intervals=[[1,3], [5,7], [8,12]], New Interval=[4,6]
Output: [[1,3], [4,7], [8,12]]
Explanation: After insertion, since [4,6] overlaps with [5,7], we merged them into one [4,7].
Example 2:

Input: Intervals=[[1,3], [5,7], [8,12]], New Interval=[4,10]
Output: [[1,3], [4,12]]
Explanation: After insertion, since [4,10] overlaps with [5,7] & [8,12], we merged them into [4,12].
Example 3:

Input: Intervals=[[2,3],[5,7]], New Interval=[1,4]
Output: [[1,4], [5,7]]
Explanation: After insertion, since [1,4] overlaps with [2,3], we merged them into one [1,4].

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/639245dffed07e597e96219f

A
117
Q

Sort Colors
Tech Interview Handbook
https://leetcode.com/problems/sort-colors/description/

A
118
Q

Given a string, identify the position of the first character that appears only once in the string. If no such character exists, return -1.

Examples

Example 1:

Input: “apple”
Expected Output: 0
Justification: The first character ‘a’ appears only once in the string and is the first character.
Example 2:

Input: “abcab”
Expected Output: 2
Justification: The first character that appears only once is ‘c’ and its position is 2.
Example 3:

Input: “abab”
Expected Output: -1
Justification: There is no character in the string that appears only once.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64b7b876aab5a6129791011e

A
119
Q

We are given an unsorted array containing n numbers taken from the range 1 to n. The array has some numbers appearing twice, find all these duplicate numbers using constant space.

Example 1:

Input: [3, 4, 4, 5, 5]
Output: [4, 5]
Example 2:

Input: [5, 4, 7, 2, 3, 5, 3]
Output: [3, 5]

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6393b0a334689e585e94a29a

A
120
Q

Given an array of positive numbers and a positive number ‘k,’ find the maximum sum of any contiguous subarray of size ‘k’.

Example 1:

Input: [2, 1, 5, 1, 3, 2], k=3
Output: 9
Explanation: Subarray with maximum sum is [5, 1, 3].
Example 2:

Input: [2, 3, 4, 1, 5], k=2
Output: 7
Explanation: Subarray with maximum sum is [3, 4].

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/636b1d083b22faa3e89b2458

A
121
Q

Given a sorted linked list, remove all the duplicate elements to leave only distinct numbers. The linked list should remain sorted, and the modified list should be returned.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/651014abfa9d768f431ff56c

A
122
Q

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.

Example 1:

Input: nums= [1, 2, 3, 4]
Output: false
Explanation: There are no duplicates in the given array.
Example 2:

Input: nums= [1, 2, 3, 1]
Output: true
Explanation: ‘1’ is repeating.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/63d8efe7f8694f3655d60712

A
123
Q

Binary Search
Tech Interview Handbook
https://leetcode.com/problems/binary-search/description/

A
124
Q

K Closest Points to Origin
Tech Interview Handbook
https://leetcode.com/problems/k-closest-points-to-origin/description/

A
125
Q

Given a sorted array, create a new array containing squares of all the numbers of the input array in the sorted order.

Example 1:

Input: [-2, -1, 0, 2, 3]
Output: [0, 1, 4, 4, 9]
Example 2:

Input: [-3, -1, 0, 1, 2]
Output: [0, 1, 1, 4, 9]

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638e39bd1756319ef156bebc

A
126
Q

Given an array of integers, identify the highest value that appears only once in the array. If no such number exists, return -1.

Examples:

Example 1:

Input: [5, 7, 3, 7, 5, 8]
Expected Output: 8
Justification: The number 8 is the highest value that appears only once in the array.
Example 2:

Input: [1, 2, 3, 2, 1, 4, 4]
Expected Output: 3
Justification: The number 3 is the highest value that appears only once in the array.
Example 3:

Input: [9, 9, 8, 8, 7, 7]
Expected Output: -1
Justification: There is no number in the array that appears only once.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/64fd56f417ec3ee519760638

A
127
Q

Given an array nums sorted in increasing order, return the maximum between the count of positive integers and the count of negative integers.

Note: 0 is neither positive nor negative.

Examples
Example 1:

Input: nums = [-4, -3, -1, 0, 1, 3, 5, 7]
Expected Output: 4
Justification: The array contains three negative integers (-4, -3, -1) and four positive integers (1, 3, 5, 7). The maximum count between negatives and positives is 3.
Example 2:

Input: nums = [-8, -7, -5, -4, 0, 0, 0]
Expected Output: 4
Justification: Here, there are four negative integers (-8, -7, -5, -4) and no positives. Thus, the maximum count is 4.
Example 3:

Input: nums = [0, 2, 2, 3, 3, 3, 4]
Expected Output: 6
Justification: This input array includes zero negative integers and six positives (2, 2, 3, 3, 3, 4). Hence, the maximum count is 6.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/663ba7065ecab6da13d0a3f2

A
128
Q

Majority Element
Tech Interview Handbook
https://leetcode.com/problems/add-binary/description/

A
129
Q

Valid Palindrome
Tech Interview Handbook
https://leetcode.com/problems/valid-palindrome/description/

A
130
Q

Maximum Depth of Binary Tree
Tech Interview Handbook
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

A
131
Q

Word Break
Tech Interview Handbook
https://leetcode.com/problems/word-break/description/

A
132
Q

A pangram is a sentence where every letter of the English alphabet appears at least once.

Given a string sentence containing English letters (lower or upper-case), return true if sentence is a pangram, or false otherwise.

Note: The given sentence might contain other characters like digits or spaces, your solution should handle these too.

Example 1:

Input: sentence = “TheQuickBrownFoxJumpsOverTheLazyDog”
Output: true
Explanation: The sentence contains at least one occurrence of every letter of the English alphabet either in lower or upper case.
Example 2:

Input: sentence = “This is not a pangram”
Output: false
Explanation: The sentence doesn’t contain at least one occurrence of every letter of the English alphabet.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63d923d6647c5c53032694bc

A
133
Q

Partition Equal Subset Sum
Tech Interview Handbook
https://leetcode.com/problems/partition-equal-subset-sum/description/

A
134
Q

Coin Change
Tech Interview Handbook
https://leetcode.com/problems/coin-change/description/

A
135
Q

Determine if a binary tree is height-balanced.

A binary tree is considered height-balanced if, for each node, the difference in height between its left and right subtrees is no more than one.

Examples:

Input:
3
/ \
9 20
/ \
15 7
Expected Output: true
Justification: Every node in the tree has a left and right subtree depth difference of either 0 or 1.
Input:
1
/ \
2 2
/ \ / \
3 3 3 3
/ \ / \
4 4 4 4
Expected Output: true
Justification: Each node in the tree has a left and right subtree depth difference of either 0 or 1.
Input:
1
/ \
2 5
/
3
/
4
Expected Output: false
Justification: The root node has a left subtree depth of 3 and right subtree depth of 1. The difference (3 - 1 = 2) exceeds 1, hence the tree is not balanced.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64f27792576dc2031a7c48c6

A
136
Q

Given a string s, reverse only all the vowels in the string and return it.

The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both lower and upper cases, more than once.

Example 1:

Input: s= “hello”
Output: “holle”
Example 2:

Input: s= “AEIOU”
Output: “UOIEA”
Example 3:

Input: s= “DesignGUrus”
Output: “DusUgnGires”

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63d9b8744bb2155485a195e9

A
137
Q

Product of Array Except Self
Tech Interview Handbook
https://leetcode.com/problems/product-of-array-except-self/description/

A
138
Q

Merge Intervals
Tech Interview Handbook
https://leetcode.com/problems/merge-intervals/description/

A
139
Q

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: sentence = “A man, a plan, a canal, Panama!”
Output: true
Explanation: “amanaplanacanalpanama” is a palindrome.
Example 2:

Input: sentence = “Was it a car or a cat I saw?”
Output: true
Explanation: Explanation: “wasitacaroracatisaw” is a palindrome.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63d92beaa4af7161a01dff44

A
140
Q

Min Stack
Tech Interview Handbook
https://leetcode.com/problems/min-stack/description/

A
141
Q

Given a string, find the length of the longest substring in it with no more than K distinct characters.

You can assume that K is less than or equal to the length of the given string.

Example 1:

Input: String=”araaci”, K=2
Output: 4
Explanation: The longest substring with no more than ‘2’ distinct characters is “araa”.
Example 2:

Input: String=”araaci”, K=1
Output: 2
Explanation: The longest substring with no more than ‘1’ distinct characters is “aa”.
Example 3:

Input: String=”cbbebi”, K=3
Output: 5
Explanation: The longest substrings with no more than ‘3’ distinct characters are “cbbeb” & “bbebi”.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6384513a635674508787c7c3

A
142
Q

Given an array of unsorted numbers, find all unique triplets in it that add up to zero.

Example 1:

Input: [-3, 0, 1, 2, -1, 1, -2]
Output: [[-3, 1, 2], [-2, 0, 2], [-2, 1, 1], [-1, 0, 1]]
Explanation: There are four unique triplets whose sum is equal to zero. smallest sum.’
Example 2:

Input: [-5, 2, -1, -2, 3]
Output: [[-5, 2, 3], [-2, -1, 3]]
Explanation: There are two unique triplets whose sum is equal to zero.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638f6ff2ae53511bdc36490d

A
143
Q

Minimum Height Trees
Tech Interview Handbook
https://leetcode.com/problems/minimum-height-trees/description/

A
144
Q

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

You must not use any built-in exponent function or operator.

For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.

Example 1:

Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.8284, and since we need to return the floor of the square root (integer), hence we returned 2.
Example 2:

Input: x = 4
Output: 2
Explanation: The square root of 4 is 2.
Example 3:

Input: x = 2
Output: 1
Explanation: The square root of 2 is 1.414, and since we need to return the floor of the square root (integer), hence we returned 1.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63d8fcaae8462840e6bde243

A
145
Q

Given a binary tree, populate an array to represent its level-by-level traversal. You should populate the values of all nodes of each level from left to right in separate sub-arrays.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6394fcd622692a384ae7dbdd

A
146
Q

Give a string s, convert it into a valid string. A string is considered valid if it does not have any two adjacent duplicate characters.

To make a string valid, we will perform a duplicate removal process. A duplicate removal consists of choosing two adjacent and equal letters and removing them. We repeatedly make duplicate removals on s until we no longer can.

Return the final string after all such duplicate removals have been made.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64903910024dfda098f3aa6f

A
147
Q

Ransom Note
Tech Interview Handbook
https://leetcode.com/problems/ransom-note/description/

A
148
Q

Merge Two Sorted Lists
Tech Interview Handbook
https://leetcode.com/problems/merge-two-sorted-lists/description/

A
149
Q

Given a binary tree and a node, find the level order successor of the given node in the tree. The level order successor is the node that appears right after the given node in the level order traversal.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/639894f2e4cab4072de783e2

A
150
Q

Spiral Matrix
Tech Interview Handbook
https://leetcode.com/problems/spiral-matrix/description/

A
151
Q

Given a string, identify the length of its longest segment that contains distinct characters. In other words, find the maximum length of a substring that has no repeating characters.

Examples:

Example 1:

Input: “abcdaef”
Expected Output: 6
Justification: The longest segment with distinct characters is “bcdaef”, which has a length of 6.
Example 2:

Input: “aaaaa”
Expected Output: 1
Justification: The entire string consists of the same character. Thus, the longest segment with unique characters is just “a”, with a length of 1.
Example 3:

Input: “abrkaabcdefghijjxxx”
Expected Output: 10
Justification: The longest segment with distinct characters is “abcdefghij”, which has a length of 10.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64fd71c1d3fee58156fd1fb5

A
152
Q

You are given a 2D matrix containing different characters, you need to find if there exists any cycle consisting of the same character in the matrix.

A cycle is a path in the matrix that starts and ends at the same cell and has four or more cells. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the same character value of the current cell.

Write a function to find if the matrix has a cycle.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638c9a7688f1e1c16f41bb3c

A
153
Q

Insert Interval
Tech Interview Handbook
https://leetcode.com/problems/insert-interval/description/

A
154
Q

3Sum
Tech Interview Handbook
https://leetcode.com/problems/3sum/description/

A
155
Q

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.

Example 1:

Input: nums= [1, 2, 3, 4]
Output: false
Explanation: There are no duplicates in the given array.
Example 2:

Input: nums= [1, 2, 3, 1]
Output: true
Explanation: ‘1’ is repeating.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63d8efe7f8694f3655d60712

A
156
Q

Given an array of numbers sorted in ascending order and a target sum, find a pair in the array whose sum is equal to the given target.

Write a function to return the indices of the two numbers (i.e. the pair) such that they add up to the given target. If no such pair exists return [-1, -1].

Example 1:

Input: [1, 2, 3, 4, 6], target=6
Output: [1, 3]
Explanation: The numbers at index 1 and 3 add up to 6: 2+4=6
Example 2:

Input: [2, 5, 9, 11], target=11
Output: [0, 2]
Explanation: The numbers at index 0 and 2 add up to 11: 2+9=11

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638ca0aa5b41522e8a2e3395

A
157
Q

Letter Combinations of a Phone Number
Tech Interview Handbook
https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

A
158
Q

Given a binary tree, return an array containing nodes in its right view. The right view of a binary tree is the set of nodes visible when the tree is seen from the right side.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63989ddfa1cb64009de121d1

A
159
Q

Given a directed acyclic graph with n nodes labeled from 0 to n-1, determine the smallest number of initial nodes such that you can access all the nodes by traversing edges. Return these nodes.

Examples
Input:

n = 6
edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
Expected Output: [0,3]

Justification: Starting from nodes 0 and 3, you can reach all other nodes in the graph. Starting from node 0, you can reach nodes 1, 2, and 5. Starting from node 3, you can reach nodes 4 and 2 (and by extension 5).

Input:

n = 3
edges = [[0,1],[2,1]]
Expected Output: [0,2]

Justification: Nodes 0 and 2 are the only nodes that don’t have incoming edges. Hence, you need to start from these nodes to reach node 1.

Input:

n = 5
edges = [[0,1],[2,1],[3,4]]
Expected Output: [0,2,3]

Justification: Node 1 can be reached from both nodes 0 and 2, but to cover all nodes, you also need to start from node 3.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/651a85ec58fd4e18cd6e4fe3

A
160
Q

Given a square matrix (2D array), calculate the sum of its two diagonals.

The two diagonals in consideration are the primary diagonal that spans from the top-left to the bottom-right and the secondary diagonal that spans from top-right to bottom-left. If a number is part of both diagonals (which occurs only for odd-sized matrices), it should be counted only once in the sum.

Examples
Example 1:
Input:
[[1,2,3],
[4,5,6],
[7,8,9]]
Expected Output: 25
Justification: Summing up the two diagonals (1+5+9+3+7), we get 25. Please note that the element at [1][1] = 5 is counted only once.
Example 2:
Input:
[[1,0],
[0,1]]
Expected Output: 2
Justification: The sum of the two diagonals is 1+1 = 2.
Example 3:
Input:
[[5]]
Expected Output: 5
Justification: Since there’s only one element, it is the sum itself.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/651bbcf208261ac5b3b37904

A
161
Q

Given a string of English lowercase and uppercase letters, make the string “good” by removing two adjacent characters that are the same but in different cases.

Continue to do this until there are no more adjacent characters of the same letter but in different cases. An empty string is also considered “good”.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64904f9272f95964a978090e

A
162
Q

You are given a 2D matrix containing only 1s (land) and 0s (water).

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

A closed island is an island that is totally surrounded by 0s (i.e., water). This means all horizontally and vertically connected cells of a closed island are water. This also means that, by definition, a closed island can’t touch an edge (as then the edge cells are not connected to any water cell).

Write a function to find the number of closed islands in the given matrix.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/638c7f5bb2790984e1934f98

A
163
Q

Any image can be represented by a 2D integer array (i.e., a matrix) where each cell represents the pixel value of the image.

Flood fill algorithm takes a starting cell (i.e., a pixel) and a color. The given color is applied to all horizontally and vertically connected cells with the same color as that of the starting cell. Recursively, the algorithm fills cells with the new color until it encounters a cell with a different color than the starting cell.

Given a matrix, a starting cell, and a color, flood fill the matrix.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6388e8887b259e5c9e8c0274

A
164
Q

Maximum Subarray
Tech Interview Handbook
https://leetcode.com/problems/maximum-subarray/description/

A
165
Q

Any number will be called a happy number if, after repeatedly replacing it with a number equal to the sum of the square of all of its digits, leads us to the number 1. All other (not-happy) numbers will never reach 1. Instead, they will be stuck in a cycle of numbers that does not include 1.

Given a positive number n, return true if it is a happy number otherwise return false.

Example 1:

Input: 23
Output: true (23 is a happy number)
Explanations: Here are the steps to find out that 23 is a happy number:

= 4 + 9 = 13
= 1 + 9 = 10
= 1 + 0 = 1

Example 2:

Input: 12
Output: false (12 is not a happy number)
Explanations: Here are the steps to find out that 12 is not a happy number:

= 1 + 4 = 5
= 25
= 4 + 25 = 29
= 4 + 81 = 85
= 64 + 25 = 89
= 64 + 81 = 145
= 1 + 16 + 25 = 42
= 16 + 4 = 20
= 4 + 0 = 4
= 16
= 1 + 36 = 37
= 9 + 49 = 58
= 25 + 64 = 89

Please note the cycle from 89 -> 89.

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63911273cf5aba4f2f70b5fe

A
166
Q

Two Sum
Tech Interview Handbook
https://leetcode.com/problems/two-sum/description/

A
167
Q

Given an unsorted array containing numbers, find the smallest missing positive number in it.

Note: Positive numbers start from ‘1’.

Example 1:

Input: [-3, 1, 5, 4, 2]
Output: 3
Explanation: The smallest missing positive number is ‘3’
Example 2:

Input: [3, -2, 0, 1, 2]
Output: 4
Example 3:

Input: [3, 2, 5, 1]
Output: 4
Example 4:

Input: [33, 37, 5]
Output: 1

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/63948c59c549a12fb2181118

A
168
Q

Given a 2D array (i.e., a matrix) containing only 1s (land) and 0s (water), count the number of islands in it.

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/6388cbb0765bb2154037ce84

A
169
Q

Given a Binary Search Tree (BST), you are required to find the smallest difference between the values of any two different nodes.

In a BST, the nodes are arranged in a manner where the value of nodes on the left is less than or equal to the root, and the value of nodes on the right is greater than the root.

Example

Example 1:

Input:
4
/ \
2 6
/ \
1 3
Expected Output: 1
Justification: The pairs (1,2), (2,3), or (3,4) have the smallest difference of 1.
Example 2:

Input:
10
/ \
5 15
/ \ \
2 7 18
Expected Output: 2
Justification: The pair (5,7) has the smallest difference of 2.
Example 3:

Input:
40
\
70
/ \
50 90
Expected Output: 10
Justification: The pair (40,50) has the smallest difference of 10.

https://www.designgurus.io/course-play/grokking-data-structures-for-coding-interviews/doc/64f2861edc5301795f81328f

A
170
Q

You are visiting a farm to collect fruits. The farm has a single row of fruit trees. You will be given two baskets, and your goal is to pick as many fruits as possible to be placed in the given baskets.

You will be given an array of characters where each character represents a fruit tree. The farm has following restrictions:

Each basket can have only one type of fruit. There is no limit to how many fruit a basket can hold.
You can start with any tree, but you can’t skip a tree once you have started.
You will pick exactly one fruit from every tree until you cannot, i.e., you will stop when you have to pick from a third fruit type.
Write a function to return the maximum number of fruits in both baskets.

Example 1:

Input: arr=[‘A’, ‘B’, ‘C’, ‘A’, ‘C’]
Output: 3
Explanation: We can put 2 ‘C’ in one basket and one ‘A’ in the other from the subarray [‘C’, ‘A’, ‘C’]
Example 2:

Input: arr = [‘A’, ‘B’, ‘C’, ‘B’, ‘B’, ‘C’]
Output: 5
Explanation: We can put 3 ‘B’ in one basket and two ‘C’ in the other basket. This can be done if we start with the second letter: [‘B’, ‘C’, ‘B’, ‘B’, ‘C’]

https://www.designgurus.io/course-play/grokking-the-coding-interview/doc/6385d2dae25dea6343fd8b19

A