Convert a Binary Tree to a Doubly Linked List
The structure of Node of a Binary Tree and Doubly linked list are same. struct Node { int data; Node* left; Node* right; } Structure of Node of a Binary Tree struct Node { int data; Node* previous; Node* next; } Structure of Node of a Doubly linked list If we treat […]
Maximum XOR value of two elements
Given an array of numbers find the maximum XOR value of two numbers in the array. Input Array = {12, 15, 5, 1, 7, 9, 8, 6, 10, 13}; Output = 15 (XOR of 5 and 10) 5 = 0101 10 = 1010 ——— XOR = 1111
Print all words in a Trie data structure
Given a collection of words stored in a Trie data structure, write a function that print all the words stored in it. For example, If the Trie is Then output should be: at ate bad bed beat beard
Trie data structure
String matching is a big research area and there are many data structure (eg. B-Tree, HashMap, Set) that help indexing of strings. There are also many algorithms used for string matching (like KMP, etc.) The major application of Trie data structure is in storing a dictionary (Collection of strings) such the searching for a word in dictionary become […]
Longest consecutive path in a Binary tree
Given a Binary tree, write code to find the length of longest path such that value of node in that path are consecutive and in decreasing order. For example is the tree is Then output should be: 3 because path (5, 4, 3) has all nodes in consecutively decreasing order. Note that Path <5, 6, 7> are […]
Rectangles in a matrix
Given a character matrix in which some of the cells have alphabets written on them while other cells are empty (empty cells contains character ‘-‘ in them). For example consider the below matrix: A – – – B – – – C All characters are unique. Set character on all empty cells in such a […]
Sum of rectangle region in a given matrix
Given a matrix of integers of order M*N and coordinates of a rectangular region in the matrix, write code to find the sum of all the elements in that rectangular region. For example, if the input matrix is And co-ordinated given are (3,4) and (6, 8), then they represent the below rectangular region whose Sum […]
Putting blocks at largest distance from each other in a row
Given a row (linear), two blocks are placed at each end and there are n empty positions between then. If ‘B’ represent block and n = 9 then the row looks like below: B _ _ _ _ _ _ _ _ _ B One blocks can be placed on each empty position. k blocks […]
Nearest number with non-decreasing digits
Given a number N given in the form of array, with each index storing one digit of the number. Write code to change the number to the greatest number less than N, such that digits of that number are in non-decreasing order. For example: INPUT ARRAY: {1, 4, 3} (representing number 143) OUTPUT : 139 […]
8 queens problem
This problem is to place 8 queens on the chess board so that they do not check each other. It can be asked in two ways: Find one of the possible solutions to place 8-queens, so that no queen can attack any other. How many different ways are possible to place 8 queens such that no […]