Suppose we have a 2D matrix mat. We have to print the matrix elements in a spiral way. At first starting from the first row (mat[0, 0]), print the whole content and then follow the last column to print, then the last row, and so on, thus it prints the elements in a spiral fashion.So, if the input is like71092916239142759911then the output will be [7, 10, 9, 1, 3, 4, 5, 11, 9, 9, 2, 9, 6, 2, 9, 2, 1, 7]To solve this, we will follow these steps:d := 0top := 0, down := row count of matrix – 1, left := 0, right := column count of matrix - 1c := 0res := a new listdirection := 0while top
Suppose we have a linked list. We have to sort the list into ascending order.So, if the input is like [5, 8, 4, 1, 5, 6, 3], then the output will be [1, 3, 4, 5, 5, 6, 8, ]To solve this, we will follow these steps:values := a new listhead := nodewhile node is not null, doinsert value of node at the end of valuesnode := next of nodesort the list valuesvalues := make a double ended queue by taking elements of valuesnode := headwhile node is not null, dovalue of node := left element of queue and delete ... Read More
Suppose we have a list of numbers called items and another value n. A salesman has items in a bag with random IDs. The salesman can delete as many as n items from the bag. We have to find the minimum number of different IDs in the bag after n removals.So, if the input is like items = [2, 2, 6, 6] n = 2, then the output will be 1 as we he can sell two items with ID 2 or ID 6, then only items with single target will be there.To solve this, we will follow these steps:c ... Read More
Suppose we have a binary tree; we have to find the depth of the second deepest leaf. If there are multiple deepest leaves, the second deepest leaf node will be the next highest one. As we know the root has a depth of 0.So, if the input is likethen the output will be 1, as the second deepest node is 3.To solve this, we will follow these steps:if root is null, thenreturn nullnodes := a new listinsert root at the end of nodescount := 0, prev := 0, now := 0while nodes is not empty, donew := a new listflag ... Read More
Suppose we have two binary trees; we have to check whether the sequence of leaves left-to-right in both trees are the same.So, if the input is likethen the output will be True as the sequence is [2, 6] for both trees.To solve this, we will follow these steps:c := a new listDefine a function inorder() . This will take root, and cif c is null, thenc := a new listif root is not null, theninorder(left of root, c)if left of root is null and right of root is null, theninsert value of root at the end of cinorder(right of root, ... Read More
Suppose we have a string and a set of delimiters, we have to reverse the words in the string while the relative ordering of the delimiters should not be changed.So, if the input is like s = "Computer/Network:Internet|tutorialspoint" delims = ["/", ":", '|'], then the output will be tutorialspoint/Internet:Network|ComputerTo solve this, we will follow these steps:words := a new listans := blank stringtemp := a map whereSeparate the words except the delimiter characters and insert them into words arrayseparate words when the character is in delimiter then add them into ans, otherwise read word from words array reversely and add ... Read More
Suppose we have linked list, we also have two values i and j, we have to reverse the linked list from i to jth nodes. And finally return the updated list.So, if the input is like [1, 2, 3, 4, 5, 6, 7, 8, 9] i = 2 j = 6, then the output will be [1, 2, 7, 6, 5, 4, 3, 8, 9, ]To solve this, we will follow these steps:prev_head := create a linked list node with value same as null and that points to nodeprev := prev_head, curr := nodeiterate through all values from 0 to ... Read More
Suppose we have an expression that holds the ternary expression, we have to evaluate the result of the expression. It supports some values like T and F for True and False and “?” and “:” characters. There are some properties:The length of the given string must be less than or equal to 10000.The conditional expressions group right-to-left.The condition will always be either T or F. So the condition will never be a digit.The result of the expression will always evaluate to T or F.So for example, if the input is like “T ? T ? F : T : T”, ... Read More
Suppose we have a list of numbers called target. Now let us consider a list X with the same length as given list and X is filled with 1s. We can perform the following operation as many times as we want: Take any index i in X and set X[i] to the current sum of X. Finally check whether X can be turned into target or not.So, if the input is like target = [5, 9, 3], then the output will be True as initially X = [1, 1, 1], then update it with total sum 3, array will be ... Read More
Suppose we have a string s and another value k, we repeatedly delete the earliest k consecutive duplicate characters, and return the final string.So, if the input is like s = "paaappmmmma" k = 3, then the output will be "ma", as when we delete three "a"s to get "pppmmmma". Then we delete three "p"s to get "mmmma". Then delete three of the four "m"s to get "ma".To solve this, we will follow these steps:do the following steps infinitely, docount := 0chars := get the unique characters from sfor each character c in chars, doif k consecutive c is in ... Read More