medium-problems

Shortest path in an unweighted graph

Problem Statement Given the directed, connected and unweighted graph G, a Source and a Destination and the task to find the shortest path possible between two given vertices in the graph. Note: The path does not contain any cycle which means path have finite number of vertices. Example Input Expected Output Path : 0 3 Implementation in …

Shortest path in an unweighted graph Read More »

Construct binary tree from inorder and postorder traversal

Problem Statement Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Sample Test Case Problem Solution Postorder traversal starts at the left subtree then right subtree and then goes to the root. Inorder traversal starts at the left subtree, then goes …

Construct binary tree from inorder and postorder traversal Read More »

Construct tree from in-order and pre-order traversal

Problem Statement Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Sample Test Case Problem Solution Preorder traversal starts at the root, goes to the left subtree and then to the right subtree. Inorder traversal starts at the left subtree, then …

Construct tree from in-order and pre-order traversal Read More »

Inorder Successor in Binary Search Tree

Problem Statement Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Sample Test Case Problem Solution A node’s inorder successor is node with least value in its right subtree i.e. its right subtree’s left most child. If right subtree of the node doesn’t exists, …

Inorder Successor in Binary Search Tree Read More »

Maximum XOR of Two Numbers in an Array

Problem Statement Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Sample Test Cases Problem Solution The Approach is that instead of finding the maximum XOR of two numbers in an array, we can find two numbers in an …

Maximum XOR of Two Numbers in an Array Read More »

Shortest Distance to Target Color

Problem Statement You are given an array colors, in which there are three colors: 1, 2 and 3. You are also given some queries. Each query consists of two integers i and c, return the shortest distance between the given index i and the target color c. If there is no solution return -1. Sample Test Cases Problem Solution The difficulty of this question is, how do we …

Shortest Distance to Target Color Read More »

Binary Search Tree to Greater Sum Tree

Problem Statement Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val. As a reminder, a binary search tree is a tree that satisfies these constraints: The left subtree of a node …

Binary Search Tree to Greater Sum Tree Read More »

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]