Are all characters in a string unique?
Here are some Java implementations checking if all the characters in a string are unique. The first solution uses HashMap to store the characters of the string:boolean hasAllUniqueChars(String str)...
View ArticleCheck if one string is permutation of another string
You have two strings and you need to decide if the first one is a permutation of the other one. The solution uses HashMap data structure where each symbol of the first string is stored as a key and its...
View ArticleCycle in LinkedList
There is an interesting technique how you can verify if there is a cycle in a LinkedList without using any additional data structure. All that is needed are two references (fast runner and slow runner)...
View ArticleArray is transformed into Binary Tree
How will you place all elements of a given array into binary tree? The given array is unsorted.Here is one recursive solution of the problem:TreeNode toTree(ArrayList array){ if (array == null ||...
View ArticleAre two Binary Trees mirror image of each other?
How can you check if two binary trees are mirror image of each other? It would mean that if you do an inorder traversal on the trees, the elements of the second tree will be visited in reversed order...
View ArticleHow to find k-th element from the end of a linked list?
The solution below uses recursion to get the number of elements in the linked list and then prints only the value at the k-th node from end of the Linked List. public static void kthElement(LinkedList...
View ArticleChinese number system
This problem comes from a Google interview (http://www.careercup.com/question?id=14942235). You probably know that the number "4" is considered unlucky number in China. Here is the problem:Number '4'...
View ArticleRotate Matrix
Here is another Google interview question (http://www.careercup.com/question?id=14891673):Given a matrix represented as int[n][n], rotate it 90 degrees clockwise in-place. (In-place means minimal extra...
View ArticleDetermine if a number is power of 2
How can you check if a number is power of 2? This is not so hard to solve and in fact there are many ways you can implement a solution. One of the most obvious solutions is probably the one where you...
View ArticleSum of nodes at odd and even levels of Binary Tree
Given a Binary Tree, write a function which calculates the difference between the sum of node values at odd levels and sum of node values at even levels, i.e. function(BinaryTree) = (sum of values of...
View ArticleReverse a LinkedList
Given a singly linked list. You have to revers it.The first solution uses recursion.Node reverseList(Node previous, Node current) { Node tmp; if (current.next == null) { current.next = previous; return...
View ArticleFibonacci numbers - 4 solutions
Find the n-th Fibonacci number. The Fibonacci numbers are integers 0, 1, 1, 2, 3, 5, 8, 13, 21, ...Wikipedia says:By definition, the first two numbers in the Fibonacci sequence are 0 and 1...
View ArticleTravel from string to string (Google Interview question)
Google Interview Question (http://www.careercup.com/question?id=14947965):Given a source string and a destination string write a program to display sequence of strings to travel from source to...
View ArticleDelete node in LinkedList
Delete a given node in a singly linked list. You don't have an access to the root node of the list. You have only access to the given node. Let's suppose this is your definition of a node. class Node {...
View ArticleBracket combinations (Google Interview Question)
Write a function that prints all combinations of well-formed brackets. The input is a number which says how many pairs of brackets will the different outputs have. For Brackets(3) the output would...
View ArticleBrackets - number of possible soltions
Given a string containing only the symbols '?', '(' and ')'. You can replace the '?' with either '(' or ')'. Your task is to write a function that tells the number of possible solutions where brackets...
View ArticleBinary Tree - sum of two nodes (Google Interview Question)
Here is another Google Interview problem:Given a BST and a value x. Find two nodes in the tree whose sum is equal x. Additional space: O(height of the tree). It is not allowed to modify the tree.The...
View ArticleBinary Tree - height and size
What is height and what is size of Binary Tree?The size of a tree is the size of the root. The size of a node is the number of descendants it has including itself. Therefore, the tree size is the...
View ArticleNᵗʰ Fibonacci Number - Matrices Multiplication
We can find the nᵗʰ Fibonacci number by multiplying matrices. Here is a good explanation of it - Nth Fibonacci Number - multiplying matrices. Let matrix A = [ [ 1 1 ] [ 1 0 ] ]. In order to find the...
View Article