Three color sort

Given a linear integer array (which has only 0s, 1s, 2s) in unsorted fashion, you need to sort that array with very minimal swaps and visit to the node. public class Answer implements ThreeColorSortInterface { @Override public void PerformThreeColorSort(ColorArray colArray) ...

Swap every two nodes in a single linked list

Write code to swap every two nodes in a single linked list. Example: Input: 2 -> 3 -> 31 -> 21 -> 5 Output: 3 -> 2 -> 21 -> 31 -> 5 public class Answer implements SwapEveryTwoNodesInLinkedListInterface{ @Override public Node SwapEveryTwoNodesInLinkedList(Node...

Base2 number system conversion

Given a number, you need to return a string which has the base 2 representation. Dont use any built-in methods to convert public class Answer implements Base2SystemInterface { @Override public String GetBase2System(int num) { // WRITE YOUR CODE HERE return ; } public...

Reverse words in a line

Given a string reverse the words in that line. public class Answer implements ReverseStringInLineInterface { @Override public String PerformReverseStringInLine(String str) { // WRITE YOUR CODE HERE return ; } public static void main(String [] args) { // write code...

Next immediate greatest number which has all the digits

Given a integer, find the next immediate greatest number which has all the digits in the given number public class Answer implements NextLargeNumberInterface { @Override public int GetNextLargeNumber(int num) { // WRITE YOUR CODE HERE return ; } public static void main(String [] args) { // write code here to test } } Example1: Input:...

Run length encoding of a string

Given a string, you need to count the consecutive characters and show the count in the string for each character. If there is only one character, you dont need to show the count. public class Answer implements RunLengthEncodingInterface { @Override public String DoRunLengthEncoding(String str) { // WRITE YOUR CODE HERE return ; } public...

Three Number Sum

Given a integer array and magic number, if there are any three numbers in the array sum up to magic number return true, else return false. Input: Array: {-10, -9, -7, 5, 0, 99} Arraylength: 6 MagicNumber: -17 Output: true Input: Array: {0, 1, 3, 5, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0} Arraylength: 14 MagicNumber: 21 Output: false public class...

Maximum ascending sequence length

Given a integer array, find the maximum length of ascending order sequence. Input: Array: {0, 1, 3, 5, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0} Arraylength: 14 Output: Maximum Ascending sequence length: 7 Input: Array: {2, -2, 2, -2, 2} Arraylength: 5 Output: Maximum Ascending sequence length: 1 public class Answer implements GetMaxAscSequenceLengthInterface...

Minimum distance between chars

Given a String, start char and end char, find the minimum distance between these two chars. If one of the strings is null or if the characters are not present we should return 0 Input 1: String: "hello world" Char1: ' ' Char 2: 'o' Output: 2 Input 2: String: "can I go to zoo" Char1: 'o' Char 2: 'o' Output: 1 public class Answer implements...

Substring Same sequence

Given a String and substing, check whether the character in substring are present in the main string in the SAME SEQUENCE. If yes, return true. Input 1: String: "Hello" Substring: "elo" Output: true Input 2: String: "hi friends" Substring: "hiiids" Output: false public class Answer implements SubStringSameSequenceInterface { @Override ...

Shuffle Merge two lists to form a new list

Given two linked lists of integers, shuffle merge those two lists to form a new linked list. Example: List1 : 1->3->5->7->9->10 List2: 2->4->6->8 Output: 1->2->3->4->5->6->7->8->9->10 Please note that after you pick elements alternatively from 2 lists, if one of the lists run out of elements,...

Remove Duplicates from the list

Given a linked list of integers, remove all the duplicate elements in the list. Elements are not necessarily in a sorted order. Input: 1->2->2->3->4->3 Output: 1->2->3->4 Input: 2->2->2->2->2 Ouput: 2 Please note that the sequence should be maintained in the list after the duplicate removal. public class...

Check if the linked list of integers is Palindrome

Given a singly linked list of integers, check if the values of the nodes are symmetrical(Palindrome). Example: 1->23->45->6->45->23->1->null is symmetrical 1->23->4->32->1->null is not symmetrical null is not symmetrical 1->null is symmetrical public class Answer implements PalindromeListInterface {...

Fully sort the list which is sorted in parts

You have a singly linked list in which the first part and the second part(not exactly split in the middle) are sorted independently. You need to return a fully sorted list merging these two parts. For example: Input: 1->3->5->7->2->4->6->8 Output: 1->2->3->4->5->6->7->8 Your Answer.java need to implement...

Finding first non repeated character in the String using Hash map

You are given a String and a hashmap. You need to use the Hashmap to find the first non repeated character in the String. Input : aaabbcd Result: c Input : null Result: ' ' Input: HelloKHello Result: K You MUST use the hashmap passed as an argument in your solution. Otherwise, your test cases won't succeed. Your Answer.java must implement the...

Ransom Note from Magazine using Hash table

Kidnapper kidnaps you and writes a ransom note. He does not write it with hand, as handwriting can put him in, so reaches to a magazine at table and creates ransom note. We need to find out given ransom string and magazine string, is it possible to create given ransom note. Kidnapper can use individual characters of words. We need to check if magazine...

Two numbers sum upto a given value using Hashmap

Find if there are two elements in the array sum up to given value. You MUST use the hashmap passed as an argumentto solve the problem. If you don't your test cases will fail. Input: {1, 2, 3, 4, 5} Value: 6 Output : true Input: {1, 2, 3, 4, 5} Value: 2 Output : false Your Answer.java should implement the following interface import java.util.Map;...

Find all the extreme nodes of a binary tree in a zigzag order

Find the extreme nodes of each level of Binary Tree in alternate order. For example, for the following binary tree, 1 2 3 4 5 6 7 8 Should have output 1->2->7->8 Your Answer.java should implement following interface. import java.util.List; public interface ExtremeNodesInterface...

Check if Binary tree is symmetric

Given a binary tree you need to check if the left sub-tree from the root is a mirror image of the right sub-tree. Please note that the symmetric nature is evaluated purely based on the structure of the tree and not based on any node values. For example. 1 2 3 4 5 Is a symmetric binary tree. Where as 1 2 ...

Boundary traverse a binary tree

Find all the boundary nodes in a given binary tree. The boundary traversal should start from root, then go clockwise (root->right->left) For example 1 2 3 4 4 6 7 8 9 1 1 2 3 3 4 This should return: 1->3->7->4->3->3->2->1->1->9->8->4->2 Your...

Check element in Binary Search Tree

Given a binary search tree with positive integers, check if given element is present in the given tree. for eg: 14 / \ / \ 3 17 / \ / \ 2 4 16 18 k = 9 Output: false k = 16 Output: true Here is sample Node.java (No need to upload this) public class Node { public int nodeValue; public Node lChild; ...

Sum of Kth Largest Element in Binary Search Tree

Given a binary search tree with positive integers, find the sum of k largest elements of the BST. (given k <= number of elements in BST) for eg: 5 / \ / \ 3 7 / \ / \ 2 4 6 8 k = 3 Sum = 6 + 7 + 8 = 21 Output: 21 Here is sample Node.java (No need to upload this) public class Node { public int nodeValue;...

Chocolate distribution problem

Given an array of n integers where each value represents number of chocolates in a packet. Each packet can have variable number of chocolates. There are m students, the task is to distribute chocolate packets such that : 1) Each student gets one packet. 2) The difference between the number of chocolates in packet with maximum chocolates and packet...

Print Palindromic Paths

Given a matrix containing lower alphabetical characters only, we need to find all palindromic paths in given matrix. A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell. You cannot go down diagonally. Input : mat[][] = {"aaab”, ...

Minimum number of hops to reach end of array or beyond

Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element. Example: Input: arr[] = {1, 3, 5, 8, 9, 2,...

Find the indices with anagram match

Given a String and a pattern find all the indices at which where we find an anagram match. For example: String : BACDGABCDA Pattern : ABCD We can find a match at {0, 5, 6} In other words, if we find 4 consecutive characters from these indices, it contains the characters needed to match the pattern. Your solution should implement the following interface: public...

Find if there are three elements in BST that sums up to zero

Given a binary tree, check if there are three elements in binary tree which will sum up to zero. Return true if triplet exists, if not return false. For example: 4 2 6 -5 3 In this binary tree, {-5, 2, 3} sum up to zero and you should return true. 0 -1 1 In this binary tree, {-1, 1, 0} sum...

Substring for given two chars

Given a string, determine the length of the smallest substring that contains exactly one occurrence of 'a' and one occurrence of 'b' If there are no such substrings, return 0 Input : ahhhxb Output : 6 Input : ahobpa Output : 3 Input : qqqop Output : 0 Answer ...

Find Max Continuous Repeating Char

Given a string, return the character that is repeated the most number of times continuously. Note: Do not count the character that occurs most overall, but count only the character that occurs most number of times continuously Note: If two characters occur the same number of times continuously, return the first such in the string. Input : AABBBC Output...

Pattern Problem | Right angle Triangle

Output Sample 1 Enter the limit: 5 * ========== * ** ========== * ** *** ========== * ** *** **** ========== * ** *** **** ***** p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco;...

Program to find FLAMES in java

Output Enter your name:  Jothibasu Enter your partner's name:  people The number is : 13 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #00cf90} people is your Affection Enter...

Odd numbers in odd index of array | Even numbers in even index of array

This logic is written only for equal number of odd and even numbers Sample Output  Enter array size: 6 Enter array elements: 1 2 3 4 5 6 After Shifting array elements : 2 1 4 3 6 5 Source Code ...

Pattern Problem | Alphabets

Test Case #1 Enter limit: Z Enter difference: 1 Output  A C E G I K M O Q S U W Y Test Case #2 Enter limit: Z Enter difference: 2 Output AD GJ MP SV p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #00cf90} Y Test Case...