Yatra Interview Experience
Read the full article
wallacepolsom

oozey mess
let's talk about Bridgerton tea, my ask is open
No title available
AnasAbdin
will byers stan first human second

pixel skylines

祝日 / Permanent Vacation
Acquired Stardust
noise dept.

izzy's playlists!
Monterey Bay Aquarium
sheepfilms

JVL
we're not kids anymore.
$LAYYYTER
hello vonnie
cherry valley forever

ellievsbear

JBB: An Artblog!

seen from Australia
seen from United Kingdom
seen from Brazil
seen from United Kingdom
seen from Brazil
seen from United States
seen from Germany
seen from United States
seen from United States
seen from United States

seen from Germany

seen from Argentina
seen from Mexico
seen from United States
seen from France

seen from United States
seen from United Kingdom
seen from United Kingdom

seen from Ecuador

seen from United States
@tutorialcup
Yatra Interview Experience
Read the full article
MAANG Interview Questions and Preparation
Read the full article
Problem Statement Vertical Order Traversal of Binary Tree LeetCode Solution says - Given the root of a binary tree, calculate the vertical order traversal of the binary tree.For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).The vertical traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.Return the vertical order traversal of the binary tree. Example 1:Input: root =Output: ,,,]Explanation: Column -1: Only node 9 is in this column. Column 0: Nodes 3 and 15 are in this column in that order from top to bottom. Column 1: Only node 20 is in this column. Column 2: Only node 7 is in this column. Example 2:Input: root =Output: ,,,,]Explanation: Column -2: Only node 4 is in this column. Column -1: Only node 2 is in this column. Column 0: Nodes 1, 5, and 6 are in this column. 1 is at the top, so it comes first. 5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6. Column 1: Only node 3 is in this column. Column 2: Only node 7 is in this column.Example 3:Input: root =Output: ,,,,] Constraints: - The number of nodes in the tree is in the range . - 0
Problem Statement Find the Winner of the Circular Game LeetCode Solution - There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 we eliminate the kth person or person with (k - 1)th index, in this case being 2(index 1). Now we have 3 people remaining 1,3,4 with starting person as 3 (index 2) or person with (k + 1)th index for the next elimination, and we know that for 3 people, the winner will be the person at the 2nd index beginning with 3 as the starting point i.e. 1(index 0) or ((k + 1) + f(n - 1)) % n, where (k +1) is starting index and f(n - 1) is the answer for (n - 1) persons. Code Java Program of Find the Winner of the Circular Game: class Solution { public int findTheWinner(int n, int k) { return findWinnerHelper(n, k - 1) + 1; } private int findWinnerHelper(int n, int k) { if (n == 1) { return 0; } return ((k + 1) % n + findWinnerHelper(n - 1, k)) % n; } } C++ Program of Find the Winner of the Circular Game: class Solution { public: int findTheWinner(int n, int k) { return findWinnerHelper(n, k - 1) + 1; } private: int findWinnerHelper(int n, int k) { if (n == 1) { return 0; } return ((k + 1) % n + findWinnerHelper(n - 1, k)) % n; } }; Python Program of Find the Winner of the Circular Game: class Solution : def findTheWinner(self, n, k) : return self.findWinnerHelper(n, k - 1) + 1 def findWinnerHelper(self, n, k) : if (n == 1) : return 0 return ((k + 1) % n + self.findWinnerHelper(n - 1, k)) % n Complexity Analysis for Find the Winner of the Circular Game LeetCode Solution Time Complexity The time complexity of the above code is O(1) because we don't do any traverse anything, we just calculate the winner based on the number of players and k. Space Complexity The space complexity of the above code is O(1) because the amount of space used is always constant no matter how big n or k is since we aren't actually traversing through the players.
Problem Statement Continuous Subarray Sum LeetCode Solution - Given an integer array nums and an integer k, return true if nums has a continuous subarray of the size of at least two whose elements sum up to a multiple of k, or false otherwise.An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k Example 1:Input: nums = , k = 6Output: trueExplanation: is a continuous subarray of size 2 whose elements sum up to 6.Example 2:Input: nums = , k = 6Output: trueExplanation: is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.Example 3:Input: nums = , k = 13Output: false Constraints: - 1
Problem Statement Count Sub Islands LeetCode Solution says that grid1 and grid2 contain only 0's (representing water) and 1's (representing land). The island means the group of 1's connected 4 directionally.An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.Example 1:Input: grid1 = ,,,,], grid2 = ,,,,]Output: 3Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.Example 2:Input: grid1 = ,,,,], grid2 = ,,,,]Output: 2Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands. Constraints: - m == grid1.length == grid2.length - n == grid1.length == grid2.length - 1
Problem Statement Palindrome Number LeetCode Solution says that -Given an integer x, return true if x is palindrome integer.An integer is a palindrome when it reads the same backward as forward. - For example, 121 is a palindrome while 123 is not. Example 1:Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left.Example 2:Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Constraints: - -231 0): b = x total = total*10+b x = x//10 if total == k: return True else: return FalsePalindrome Number Java LeetCode Solution: class Solution { public boolean isPalindrome(int x) { int total = 0; int k = x; while(x > 0){ int b = x; total = total*10 + b; x = x/10; } if(total == k) return true; return false; } } Complexity Analysis : Time complexity: O(N). Space complexity: O(n).Similar Problem: https://www.tutorialcup.com/interview/string/shortest-palindrome.htm
Problem Statement Range Sum Query 2D - Immutable LeetCode Solution - Given a 2D matrix, handle multiple queries of the following type: - Calculate the sum of the elements of the matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).Implement the NumMatrix class: - NumMatrix(int matrix) Initializes the object with the integer matrix matrix. - int sumRegion(int row1, intcol1, int row2, int col2) Returns the sum of the elements of the matrix inside the rectangle defined by its upper left corner (row1, col) and lower right corner (row2, col2). Example 1:Input, , , , ]], , , ]OutputExplanation NumMatrix numMatrix = new NumMatrix(, , , , ]); numMatrix.sumRegion(2, 1, 4, 3); return 8 (i.e sum of the red rectangle) numMatrix.sumRegion(1, 1, 2, 2); return 11 (i.e sum of the green rectangle) numMatrix.sumRegion(1, 2, 2, 4); return 12 (i.e sum of the blue rectangle) Approach Idea: First, we make a cumulative sum for each row. Then, we use this new cumulative array and subtract the last int in our new "sumRegion" row from the first number in that "numMatrix" row. This shows us how much each row is and after we find the sums of each row, add them together to figure out the sum of the "sumRegion". For regions that are taller than they are wide, do this same process but with the columns instead. The link down below shows the process written out. Code Python Program of Range Sum Query 2D - Immutable class NumMatrix : dp = None def __init__(self, matrix) : self.dp = * (len(matrix)) for _ in range(len(matrix))] self.populateArray(matrix, self.dp) def populateArray(self, arr, dp) : i = 0 while (i < len(arr)) : j = 0 while (j < len(arr)) : if (j == 0) : dp = arr else : dp = arr + dp j += 1 i += 1 def sumRegion(self, row1, col1, row2, col2) : if (row2 >= len(self.dp) or col2 >= len(self.dp) or row1 < 0 or col1 < 0) : return -1 sum = 0 i = row1 while (i 0 else 0)) i += 1 return sum Java Program of Range Sum Query 2D - Immutable class NumMatrix {int dp;public NumMatrix(int matrix) { dp = new int.length]; populateArray(matrix, dp); }private void populateArray(int arr, int dp) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (j == 0) { dp = arr; } else { dp = arr + dp; } } } }public int sumRegion(int row1, int col1, int row2, int col2) { if (row2 >= dp.length || col2 >= dp.length || row1 < 0 || col1 < 0) return -1; int sum = 0; for (int i = row1; i 0 ? dp : 0)); } return sum; } } Complexity Analysis for Range Sum Query 2D - Immutable LeetCode Solution Time Complexity The time complexity of the above code is O(n^2) because there is a nested for loop. Space Complexity The space complexity of the above code is O(n) because we are using an array to store the prefix values of the original array.
Problem Statement Longest Common Subsequence LeetCode Solution - Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. - For example, "ace" is a subsequence of "abcde".A common subsequence of two strings is a subsequence that is common to both strings. Example 1:Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3.Example 2:Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3.Example 3:Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0. Explanation - The idea is to use dynamic programming. - Create a 2D DP array of n+1*m+1 dimensions - I am using the bottom up dp approach so i have iterated from text1.length()-1 to 0 and text2.length()-1 to 0 - if the characters match,we do 1+ the diagnal value dp - else we find the max between left and bottom value max(dp,dp) - return the dp as it will have the value for longest common SubsequenceCode Longest Common Subsequence Leetcode Java Solution: class Solution { public int longestCommonSubsequence(String text1, String text2) { int dp = new int; for(int i= text1.length()-1;i>=0;i--){ for(int j = text2.length()-1;j>=0;j--){ char ch1 = text1.charAt(i); char ch2 = text2.charAt(j); if(ch1==ch2) // diagnal dp= 1+dp; else// left,down dp = Math.max(dp,dp); } } return dp; } } C++ Solution: class Solution { public: int longestCommonSubsequence(string text1, string text2) {// // base case // if( text1.length() == 0 || text2.length() == 0) return 0;// // recursive call // if( text1 == text2) return 1 + longestCommonSubsequence( text1.substr(1) , text2.substr(1) );// else return max( longestCommonSubsequence( text1.substr(1) , text2 ) , longestCommonSubsequence( text1 , text2.substr(1) )); int n = text1.length(); int m = text2.length(); int dp; for( int i = 0 ; i
Flipping an Image LeetCode Solution
Problem Statement
Flipping an Image LeetCode Solution - We are given a matrix of size n. We need to perform 2 tasks- - flip the image horizontally: it means each row of the given matrix is reversed - invert the image: make all 0’s to 1’s & vice versa Return the resulting matrix. Given matrix contains only 0 or 1 and size of image is ≤ 20.
Examples & Explanations
Example 1: Input: image = ,,] Output: ,,] Explanation: First reverse each row: ,,]. Then, invert the image: ,,] Example 2: Input: image = ,,,] Output: ,,,] Explanation: First reverse each row: ,,,]. Then invert the image: ,,,]
Approach
After reviewing some examples you will notice the following patterns: 1) Look at the first and last value of the row. If they are the same (1,1 or 0,0), they will be flipped in the output. If they are different (1,0 or 0,1), they do not change. Work your way inward to the middle of the list applying this rule. 2) If the row has an odd number of entries, the middle value always flips. For example if len(row) = 5, then row must change values. Bitwise XOR --> 0^1 = 1, 1^1 =0 Let i be the index at the beginning of the row, and j be the index at the end of the row. If the values at these indices (row and row) are equal, flip their values using XOR ^. If the values are not equal, do nothing and move i and j closer to the middle. When i == j , the code still executes as it should. The question has 2 parts- - Flipping the image: we can simply use another array or vector to store matrix and reverse it for all rows and store the answer in another matrix res - Inverting the image: this is an easy task, simply check the value of res and change the value This approach requires us to declare another matrix of size n x n. Can we do better? We can achieve flipping in place by swapping the elements by pivoting the middle element of every row in the image. For each row, keep the middle element in place and swap the ith element with the (n-1-i)th element. We will use XOR to invert the elements. We know that 1^1 = 0 & 1^0 = 1
Code
C++ code for Flipping an Image class Solution { public: vector flipAndInvertImage(vector& image) { int n = image.size(); for(auto &row: image) { for(int i=0; i Read the full article
Partition Labels LeetCode Solution
Problem Statement
Partition Labels LeetCode Solution - You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s. Return a list of integers representing the size of these parts.
Examples and Explanation
Example 1: Input: s = "ababcbacadefegdehijhklij" Output: Explanation: The partition is "ababcbaca", "defegde", "hijhklij". This is a partition so that each letter appears in at most one part. A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts. Example 2: Input: s = "eccbbbbdec" Output: Explanation: The given string can not partitioned further as the letters will be repeated if partitioned.
Algorithm
Let’s look at example 1, we can notice that if we are including ‘a’ in the first partition then we need to include all the ‘a’s appearing in the string. So, we will include the substring from the first index till the last index of occurrences of ‘a’. In the second partition, "defegde" , we notice that including substring from the first index of ‘d’ till the last index of ‘d’ will not suffice. This means that we need to include all the last indices of all letters appearing between the first and last indices of ‘d’ as well.
Code
C++ program for Partition Labels class Solution { public: vector partitionLabels(string s) { map index; for(int i=0; i Read the full article
Range Sum Query 2D - Immutable Leetcode Solution
Problem Statement
Range Sum Query 2D - Immutable Leetcode Solution - Given a 2D matrix matrix, handle multiple queries of the following type: - Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Implement the NumMatrix class: - NumMatrix(int matrix) Initializes the object with the integer matrix matrix. - int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
Example
Input , , , , ]], , , ] Output Explanation NumMatrix numMatrix = new NumMatrix(, , , , ]); numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle) numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle) numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
Constraints
- m == matrix.length - n == matrix.length - 1 Read the full article
Amazon Coding Interview Questions
Read the full article
Longest Palindromic Substring LeetCode Solution
Problem Statement
The Longest Palindromic Substring LeetCode Solution - “Longest Palindromic Substring” states that You are Given a string s, return the longest palindromic substring in s. Note: A palindrome is a word that reads the same backward as forwards, e.g. madam.
Example:
s = "babad" "bab" Explanation: All the unique palindromic substrings are: "b", "a", "d", "bab", "aba". Out of these, “bab” and “aba” are the longest substrings. s = "cbbd" "bb" Explanation: All the unique palindromic substrings are: "c", "b", "d", "bb". Out of these, “bb” is the longest substring.
Brute Force Solution
Idea: We can check all the substrings and check which substrings are palindrome, then take the longest among them.
Code:
C++ Program of Longest Palindromic Substring LeetCode Solution #include using namespace std; bool check(string &s, int i, int j) { while (i max_len) { max_len = j - i + 1; starting_index = i; } } } } return s.substr(starting_index, max_len); } int main() { string s = "babad"; cout Read the full article
Longest Palindromic Substring LeetCode Solution
Problem Statement
The Longest Palindromic Substring LeetCode Solution - “Longest Palindromic Substring” states that You are Given a string s, return the longest palindromic substring in s. Note: A palindrome is a word that reads the same backward as forwards, e.g. madam.
Example:
s = "babad" "bab" Explanation: All the unique palindromic substrings are: "b", "a", "d", "bab", "aba". Out of these, “bab” and “aba” are the longest substrings. s = "cbbd" "bb" Explanation: All the unique palindromic substrings are: "c", "b", "d", "bb". Out of these, “bb” is the longest substring.
Brute Force Solution
Idea: We can check all the substrings and check which substrings are palindrome, then take the longest among them.
Code:
C++ Program of Longest Palindromic Substring LeetCode Solution #include using namespace std; bool check(string &s, int i, int j) { while (i max_len) { max_len = j - i + 1; starting_index = i; } } } } return s.substr(starting_index, max_len); } int main() { string s = "babad"; cout Read the full article
Merge Two Binary Trees LeetCode Solution
Problem Statement
Merge Two Binary Trees LeetCode Solution - You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree. Return the merged tree.
Example
Test Case 1: Input: root1 = root2 = Output:
Explanation
After merging the Binary trees the output tree becomes . Approach Idea We can traverse both the given trees in a preorder fashion. At every step, we check if the current node exists(isn't null) for both the trees. If so, we add the values in the current nodes of both the trees and update the value in the current node of the first tree to reflect this sum obtained. At every step, we also call the original function mergeTrees() with the left children and then with the right children of the current nodes of the two trees. If at any step, one of these children happens to be null, we return the child of the other tree(representing the corresponding child subtree) to be added as a child subtree to the calling parent node in the first tree. At the end, the first tree will represent the required resultant merged binary tree.
Code for Merge Two Binary Trees LeetCode Solution
Java Program class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { if (root1 == null) return root2; if (root2 == null) return root1; TreeNode node = new TreeNode(root1.val + root2.val); node.left = mergeTrees(root1.left, root2.left); node.right = mergeTrees(root1.right, root2.right); return node; } } C++ Program class Solution { public: TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) { if (!root1 || !root2) return root1 ? root1 : root2; TreeNode* node = new TreeNode(root1->val + root2->val); node->left = mergeTrees(root1->left, root2->left); node->right = mergeTrees(root1->right, root2->right); return node; } };
Complexity Analysis for Merge Two Binary Trees LeetCode Solution
Time Complexity will be O(m). A total of m nodes need to be traversed. Here, m represents the minimum number of nodes from the two given trees. Space Complexity will be O(m). The depth of the recursion tree can go up to m in the case of a skewed tree. In an average case, depth will be O(logm). Reference: https://en.wikipedia.org/?title=Pre-order_traversal Read the full article
Strobogrammatic Number LeetCode Solution
Problem Statement
Strobogrammatic Number LeetCode Solution - Given a string num which represents an integer, return true if num is a strobogrammatic number. A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Example
Test Case 1: Input: num = "69" Output: true Test Case 2: Input: num = "692" Output: false
Explanation
i) For the first test case if we rotate "69" by 180 degrees, it's the same. ii) For the second test case if we rotate "692" by 180 degrees, it's not the same number. Approach Idea We need to determine what each digit becomes when rotated by 180 degrees. There are three possibilities for each digit: - it becomes invalid - it stays the same - it becomes a different digit We'll consider a digit to be rotatable if, and only if, that digit becomes a valid digit when rotated. If we think carefully, we can identify that 0, 1, 6, 8, 9 are rotatable as only these digits are valid when it is rotated upside down by 180 degrees. So if the number contains any digit other than these, we can easily say that it is not a strobogrammatic number. For other digits, we need to check if their rotation is the same as the number at its counterpart.
Code for Strobogrammatic Number LeetCode Solution
Java Program class Solution { public boolean isStrobogrammatic(String num) { Map map = new HashMap(); map.put('6', '9'); map.put('9', '6'); map.put('0', '0'); map.put('1', '1'); map.put('8', '8'); int l = 0, r = num.length() - 1; while (l Read the full article