# Find the sum of the largest increasing branch in an n-ary tree

Finding the sum of the largest increasing branch in an n-ary tree can be a difficult task. This article provides an example C++ program that can help you find the sum of the largest increasing branch in an n-ary tree. An n-ary tree is a tree in which every node has at most n children.Read More »

# Top 20 easy to medium algorithm interview questions

In today’s world competition is everywhere. Any average software company is looking for candidates who are competitive. The simplest way for companies to evaluate competitiveness in candidates is by asking them a couple of algorithm interview questions.

A typical interview consists of two algorithm interview questions within 45 to 60 minutes of the timeframe. Throughout this time interviewer evaluates the candidate’s thought process and his ability to convert the thoughts to code or algorithm.

On this page, we are providing 20 easy to medium algorithms with a detailed explanation of the algorithm along with code (mostly written in C/C++ programming language).

Longest palindromic substring length of a given string (recursion and dynamic programming approach)

Count of all subsets with given sum in an unsorted integer array

Topological sort of an acyclic directed graph

Best complexity edit distance algorithm with an example

K-th largest or smallest element in an unsorted array of integers

Find the maximum subarray sum in an unsorted integer array

Find the median of an unsorted stream of integers at any given time

Reverse alternate group of K nodes in a singly linked list

Find root to leaf path whose sum is maximum in a binary tree

Print binary tree nodes in vertical order (vertical order traversal)

Find verticals sum of binary tree

Lowest common ancestor

Find mirror of a binary tree

Find the diameter of a given binary tree

Convert binary tree to its sum tree in-place

Check if given binary tree is complete

Find height of given binary tree

Find given two binary trees are identical

Find the median of two sorted arrays

Find if there exists a pair with a given sum in a sorted array.

To get more understanding of these algorithms, focus more on understanding the algorithms and testing with different edge cases (or requirements) and then focus on writing clean coding.

While writing the code re-check your code for any functional or programming errors.

Please let us know in comments if you have any algorithms that you want us to explain. All the very best for your interview prepation.

# Print missing and duplicate numbers in the given sequence of numbers.

Given a range of natural numbers where some numbers are repeated and some are missing in the range. Our aim is to find missing numbers and repeated numbers. Let us discuss the problem with an example. So for the input {2, 3, 3, 5, 5, 8, 2} our output should be as follows. This problemRead More »

# Auto-suggestion implementation using Trie Data Structure.

Trie data structure has many useful applications, one of the important applications of the trie is auto-suggestion when user inputs using the keyboard. Let us assume we have a dictionary that contains “APPLE”, “APE”, “GOD”, “GOOD”, “GUN”. So, when a user enters “GO” then our auto-suggestion suggests “GOD”, “GOOD” as a suggested list of words.Read More »

# Trie Data Structure

Trie data structures are useful to process and search in large character data sets. It takes only orders of word length to search a word in length file or a million words dictionary. Some examples where Trie’s use. In finding words in large files. In finding any word in the large dictionary of words. InRead More »

# Find the longest substring with given max unique characters.

Given a string and an integer representing max unique characters, we aim to find the longest substring that does not exceed max unique characters. Let us understand this with an example. We can solve this problem by iterating over each character set in the array and gets the difference in character change. Let’s see thisRead More »

# Find the nearest integer of a given number in the binary search tree.

Given a Binary Search Tree (BST) contains integers and an integer. We aim to find the nearest node to input integer in the BST. Let us understand this with an example. Let’s see how our solution works to get the nearest node to the target integer. This algorithm runs with O(H), where is ‘H’ isRead More »

# Clone given Graph

Given a graph with an adjacency list, we aim to clone the complete graph with edges. Cloning does creates a new memory for similar data. Let us understand this with a three-node example graph. We can solve this problem using either DFS or BFS. Let’s solve this problem using DFS. Our Graph object contains theRead More »

# An algorithm to move all zeros to rightmost in an integer array.

Given an integer array, which contains some ‘k’ zeros. We aim to move all ‘k’ zeros to the right of most of the integer array. Let us see this with an example. Algorithm Iterate through each integer in the array and take a variable “pos,” representing the end of the array at first. If theRead More »

# Find if there exists a pair with a given sum in a sorted array.

Given an array of integers which are in ascending order and a sum. Our aim is to find if there is any pair of integers which total is equal to input sum. Let us discuss this problem with below example. Above example contains numbers 4 & 9 which sum is equal to our input. WeRead More »

# Parentheses matching problem.

Given a set of parentheses (a string expression), we aim to validate those parentheses. Let us see this with some examples. We can solve this problem using stack data structure. Here we have taken three sets of braces which are ‘{}’, ‘()’ and ‘[]’. Algorithm Check if stack not empty and current bracket character isRead More »

# Find the starting point of a sorted rotated array.

Given an ascending sorted rotated array, we aim to get the starting point of the array (the smallest element in the input array). Let us discuss this with an example. We use binary search to find the starting point of the sorted array. Let us see how this works. As we are using binary searchRead More »

# Find the number of visible boxes after inserting smaller size boxes into bigger size boxes.

Given an array of integers, each number represents the size of its box. A smaller size box can fit into a larger size box, whereas the same or larger size boxes can’t fit into one another. Let us see this problem with the below example. If we observe the above example, whatever the sized boxRead More »

# Find all possible combinations of decodings of a number.

Given an encoding number as explained below, we aim to print all combinations of decoding strings to that input number. The encoding number is as follows. Defining a recursion for this problem is very simple. Let us see the recursion tree for the input number. Let us now write the code for this recursion algorithm.Read More »

# Given two linked lists, both represent a number need to find its sum linked list.

As described in problem statement, we have two linked lists represents as decimal numbers. We aim to find its sum as a linked list. Let us understand this with an example. We can solve this problem in two different ways. Digit by digit sum calculation. Converting a list to the number and calculate the sumRead More »