Tagged: Beginner

Find the Size of a Binary Tree without Recursion

Objec­tive: Given a binary tree, Write an non-recursive algo­rithm to find the size of the tree. Note : Size of the tree is num­ber of nodes in the tree Approach: In our ear­lier post (link) we…

Delete a Node in the Middle of a linked list, Given only access to that Node

Objec­tive: Write a pro­gram to Delete a Node in the Mid­dle of a linked list, Given only access to that Node Exam­ple: Orig­i­nal List : ->1->2->8->3->7->0->4 After Delet­ing the mid node (say 7) : ->1->2->8->3->0->4…

Find the n’th Node from the end of a given Linked List

Objec­tive: Given a linked list and inte­ger ‘n’, write an algo­rithm to find the nth node from the end in the Linked List. Exam­ple: Orig­i­nal List : ->1->2->8->3->7->0->4 Out­put : 3rd Ele­ment from the…

Remove Duplicates from an Unsorted Linked list

Objec­tive: Write a pro­gram to remove the dupli­cates from an unsorted linked list Exam­ple: Input Linked List : 1->2->2->4->3->3->2 Out­put : 1->2->4->3 Input: An unsorted linked list Out­put: Linked list with no dupli­cates. Approach:…

Check if one string is Rotation of another string

Objec­tive: Write an algo­rithm to check if one string is Rota­tion of another string. This ques­tion has been asked in the Ama­zon inter­view. Exam­ple: Input Strings : ‘sum­it­jain’ and ‘tjain­sumi’ Out­put : true Input…

String Compression using count of repeated characters — Run Length Encoding

Objec­tive: Write an algo­rithm to com­press the given string by using the count of repeated char­ac­ters and if new com­pressed string length is not smaller than the orig­i­nal string then return the orig­i­nal string.…

Find Whether Two Strings are Permutation of each other

Objec­tive: Given Two Strings, check whether one string is per­mu­ta­tion of other Input: Two Strings Out­put: True or false based on whether strings are per­mu­ta­tion of other or not. Exam­ple: “sumit” and “tiums” are per­mu­ta­tions of…

Find Whether Given String is palindrome or Not.

Objec­tive : Write an algo­rithm to find Whether Given String is palin­drome or Not. Input:  A String, Out­put: true or false on whether string is palin­drome or not Approach: Use recur­sive approach Com­pare first and last…

Find a peak element in a Given Array

Objec­tive : In this arti­cle we will dis­cuss an algo­rithm to Find a peak ele­ment in a Given Array. We will see the recur­sion tech­niques to solve this prob­lem. Peak Ele­ment: peak ele­ment is…

Find two Missing Numbers in a Sequence of Consecutive Numbers

Objec­tive : Write an algo­rithm to find two Miss­ing Num­bers in a Sequence of Con­sec­u­tive Num­bers Input:  Array, arrA[] with two miss­ing num­bers and Range Out­put : Two miss­ing num­bers Approach: Approach is very simple,…

Find a Missing Number From a Sequence of Consecutive Numbers

Objec­tive : You have been asked to Write an algo­rithm Find a Miss­ing Num­ber From a Sequence of Con­sec­u­tive Num­bers Input:  Array, arrA[] with a miss­ing num­ber and Range Out­put : miss­ing num­ber Approach: Approach is…

Hash Table Implementation

Objec­tive : To imple­ment a Hash Table Input:  A set of pairs of keys and val­ues Approach: Cre­ate a Hash Table Hashtable<Integer, String> ht = new Hashtable<Integer, String>(); Insert val­ues in hash table using put(key,value)…

Binary Search

Objec­tive : Write an algo­rithm to find an ele­ment in an sorted array Input: A sorted array, arrA[] and an key Out­put : Return true if ele­ment is found, else false. Approach: The idea is to…