luxehoogl.blogg.se

Basic data structures and algorithms interview questions
Basic data structures and algorithms interview questions






basic data structures and algorithms interview questions

The following are steps to print Bottom View of Binary Tree.ġ. (That is why if we see a smaller element in step a), weĬ) push pre to stack (All elements in stack are in decreasing Make the last removed item as new root (toĪt this point, pre is greater than the removed root Here we find next greater element and after finding next greater, if we find a smaller element, then return false.Ī) If pre is smaller than current root, return false.ī) Keep removing elements from stack while pre is greater This problem is similar to Next (or closest) Greater Element problem. Time Complexity of the above solution is O(n2)Īn Efficient Solution can solve this problem in O(n) time. (ii) Recursive calls for the subarrays pre and (i) All values after the above found greater value are Now we can read the shortest path from source to target by reverse iteration:ģ while prev is defined: // Construct the shortest path with a stack SĤ insert u at the beginning of S // Push the vertex onto the stackĥ u ← prev // Traverse from target to sourceĦ insert u at the beginning of S // Push the source onto the stackĪ Simple Solution is to do following for every node pre starting from first one.ġ) Find the first greater value on right side of current node. If we are only interested in a shortest path between vertices source and target, we can terminate the search after line 15 if u = target. 5 for each vertex v in Graph: // InitializationĦ dist ← INFINITY // Unknown distance from source to vħ prev ← UNDEFINED // Previous node in optimal path from sourceĨ add v to Q // All nodes initially in Q (unvisited nodes)ġ0 dist ← 0 // Distance from source to sourceġ3 u ← vertex in Q with min dist // Node with the least distanceġ7 for each neighbor v of u: // where v is still in Q.ġ9 if alt < dist: // A shorter path to v has been found








Basic data structures and algorithms interview questions