Algorithms Interview Practice Basic – Breathe First Traversal In the iterative way You can only do breathe first traversal in iterative way. Though you can still make it recursive by calling itself in each loop, but you will not benefit from

git Interview Practice Basic – Depth First Traversal There are 4 ways to do a depth first traversal, depends on how you would like to do it. .gist table { margin-bottom: 0; }

find Interview Practice Basic - Snapsack Problem Snapsack problem refers to the situation you want to find a combination of items that can fill up a container. Each items should be attached with a value, and there should be a

app Interview Practice Extra 07 - Universal Value Binary Tree Question Design an algorithm to verify that a tree is a universal value binary tree. Universal value binary tree means all value in that tree is the same. Solution There is two approach

5 cent Interview Practice Extra 06 - Vending Machine Question This is an actual question I encountered in an Amazon phone interview in November 2013. You are going to design the money changing algorithm for a vending machine. That is, after any

extra Interview Practice Extra 05 – Identify Prime Numbers Question Write an algorithm to identify prime numbers from a list of numbers ranging 0-100. Solution The main question is actually to write a program to check if a number is prime. There

extra Interview Practice Extra 04 – Remove Node from Singly Linked List Question How do you remove a node from a singly linked list, given only that node? Head node is not given. Solution Set next of this node to the next of the next

binary Interview Practice 28 - Counting One in Binary Expression Question Given a number, find the number of 1 in the number’s binary expression. For example, binary express of 10 is 1010. So the number of 1 in it is 2. Solution

cha Interview Practice 26 - String Right-rotation Question Write a program to right-rotate a string by m characters. Right-rotating a string means moving m characters at the left of string to the right. Is it required the time complexity is

cha Interview Practice 25 - Longest Consecutive Digits Question Given a function prototype: int continumax(char *output_string,char *input_string). Implement it to find the longest consecutive digits. This function must return the length of the longest digits. The found

interview Interview Practice 24 - Reverse a Linked List Question Reverse a linked list. Sample node structure class Node: value = None next = None def init(self, value, next_node=None): self.value = value self.next = next_node # reverse the linked list and

how Interview Practice 22 - Card Guessing Question Consider there are 4 blue and 4 red cards. Host gets 2 cards randomly, no one knows what cards they are. Then he places 2 random cards at the forehead of each

app Interview Practice 21 - Summing Combinations Question Given a integers m and n, generate all combination within 1 to n that would give the sum m. For example, for m=5 and n=5, the combinations are {5}, {4+

cha Interview Practice 20 - Convert String to Integer Question Convert the inputted string to an integer. For example, “345” will output 345. Solution Though it looks simple, in fact it is pretty tricky. First we need to make the concept clear.

construct Interview Practice 19 - Fastest Way to Calculate Fibonacci Sequence Question Construct an algorithm so that the Nth item of Fibonacci Sequence can be found in the shortest time. Definition of Fibonacci Sequence is: = 0 , when n = 0 f(n) = 1 , when n

circle Interview Practice 18 - Last Surviving Number in Loop Consider Consider there is a list containing N numbers, and which formed a ring. That is, item n+1 is item 1. Construct an algorithm such that it traverses through the ring, and

app Interview Practice 17 - Find The String Appeared Once Question Given a string, write an algorithm to find the character inside that appeared only once. For example, the result for string “abaccdeff” is b. Solution We can use a hashtable to count

app Interview Practice Extra 03 - Verify Binary Tree with Same Value Question Verify whether all nodes have the same value in a binary tree. Solution We can traverse the tree with our usual way, like depth-first or breadth-first algorithm. Then pass a value, probably

extra Interview Practice Extra 02 - Duplicates in List Question Write an algorithm to remove duplicated node from a linked list. Solution There are many ways to do it. For the first one, as the simplest one, we could use 2 loops

extra Interview Practice Extra 01 - Find Loop in Linked List Question Validate a linked list whether there is a loop in it. That is, there is a node in a linked list with the next pointer pointing to a node ahead in the

app Interview Practice 16 - Print Binary Tree Layer-by-layer Question Print a binary tree layer-by-layer from top to bottom, and from left to right for each layer. Solution Yes, it’s a simple task. We can use breadth-first search, and which means

app Interview Practice 15 - Mirror Image of Binary Tree Question Construct 2 algorithms to make mirror image of any binary tree inputs, one of them using recursive method, another one using looping method. Mirror image means a binary tree that is the

find Interview Practice 14 - Find Integer With Wanted Sum Question Given a sorted integer list and an integer, find two integer in the list such that the sum of the two integers equals the given integer. It is required that the time

cha Interview Practice 13 - Last Kth Node of Linked List Question Given a linked list, find the Kth last node in a linked list. The last 0th node is the tail node in the linked list. Solution Easy task. Construct 2 pointers: P1

interview Interview Practice 12 - Sum from 1 to N Unconventionally Question Sum up 1 to n without using division, multiplication, for loop, while loop, if else, switch and condition statement (A?B:C). Solution 1 Actually, for loop can be simulated through making