Consider we have a linked list, and we have to check whether there is any cycle or not. To represent the cycle in the given linked list, we will use one integer pointer called pos. This pos represents a position in the linked list where tail is connected. So if pos is -1, then there is no cycle present in the linked list. For example, the linked list is like [5, 3, 2, 0, -4, 7], and pos = 1. So there is a cycle, and tail is connected to the second node. The constraint is that we cannot modify ... Read More
Suppose we have a 2D board containing X and O. Capture all regions surrounded by X. A region is captured by changing all Os into Xs in that surrounded region.XXXXXOOXXXOXXOXXAfter running the output will beXXXXXXXXXXXXXOXXTo solve this, we will follow these steps −If board is not present, then return blank boardfor i in range 0 to number of rows – 1 −if board[i, 0] = ‘O’, then make_one(board, i, 0)if board[i, length of row - 1] = ‘O’, then make_one(board, i, length of row – 1)for i in range 0 to number of cols – 1 −if board[0, i] = ... Read More
Suppose we have given a binary tree in which each node holds an integer key. We have to find the paths that sum to a given value. The path should start from root to leaf. We have to find the path where the sum is same.If the tree is like [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1], and sum is 22, then it will be −The paths are [[5, 4, 11, 2], [5, 8, 4, 5]].To solve this, we will follow these steps −Use the dfs function to solve this problem, the dfs is ... Read More
Suppose we have the inorder and postorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the postorder and inorder sequences are [9, 15, 7, 20, 3] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps -Suppose the method is called buildTree with preorder and inorder listsroot := last node from the postorder, and delete first node from postorderroot_index := index of root.val from the inorder listleft or root := buildTree(subset of inorder from root_index + 1 to end, postorder)right or root := buildTree(subset of ... Read More
Suppose we have the inorder and preorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the preorder and inorder sequences are [3, 9, 20, 15, 7] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps −Suppose the method is called buildTree with preorder and inorder listsroot := first node from the preorder, and delete first node from preorderroot_index := index of root.val from the inorder listleft or root := buildTree(preorder, subset of inorder from 0 to root_index)right or root := buildTree(preorder, subset of inorder ... Read More
Suppose we have a linked list. We have to reverse the nodes from position m to n. We have to do it in one pass. So if the list is [1, 2, 3, 4, 5] and m = 2 and n = 4, then the result will be [1, 4, , 3, 2, 5]Let us see the steps −There will be two methods, the reverseN() and reverseBetween(). The reverseBetween() will work as main method.define one link node pointer called successor as nullThe reverseN will work as follows −if n = 1, then successor := next of head, and return headlast ... Read More
Suppose we have a set of numbers; we have to generate all possible subsets of that set. This is also known as power set. We have to keep in mind that the elements may be duplicate. So if the set is like [1, 2, 2], then the power set will be [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]Let us see the steps −Define one array res and another set called xWe will solve this using recursive approach. So if the recursive method name is called solve(), and this takes index, one temporary array, and the array of ... Read More
Suppose we have a list of some elements. We have to remove all elements that have occurred more than once. So only the distinct elements will remain in the list. So if the list is like [1, 1, 1, 2, 2, 3, 5, 6, 6, 7, 8], then the output will be [3, 5, 7, 8], all other elements are present more than once.Let us see the steps −Create a dummy node with value -1, prev := NULL, dummyPtr := dummywhile head is not nullif next of head is present or the value of head is not same as the ... Read More
Consider we have an array sorted in ascending order. That is rotated at some pivot unknown to us beforehand. For example, if the array is like [0, 0, 1, 2, 2, 5, 6], this might become [2, 5, 6, 0, 0, 1, 2]. We have a target value to search. If that is found in the array, then return true, otherwise return false. So if the array is like [2, 5, 6, 0, 0, 1, 2], and target is 0, then the output will be 0Let us see the steps −low := 0 and high := size of arraywhile low ... Read More
Suppose we have a sorted array nums, we have to remove the duplicates in-place such that duplicates elements will appear at most twice and return the new length. To do this task we cannot take extra space. We have to solve this with O(1) amount of space. For example, if the array is like [0,0,0,1,1,1,1,2,3,3], then the output will be [0,0,1,1,2,3,3], its length is 7Let us see the steps −len := 2 and n := size of arrayif n
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP