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/