## 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 target value that means a container is filled up. For example, a container has 25 spaces and there are items that will…

## 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 purchase, the machine makes change to users with a combination of coins. And the machine only have 3 types of coins: nickel…

## 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 are 4 situations. 1. If number is 0 or 1, it is not prime. 2. if the number is 2, it is…

## 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 node. node->next = node->next->next; Reference Glassdoor

## 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 to compare each element with all other elements. However, this takes forever: O(n²). The second way Reference http://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/

## 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 we need a queue, see reference. levelorder(root) q = empty queue q.enqueue(root) while not q.empty do node := q.dequeue() visit(node) if node.left ≠…

## 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 horizontal reflection of the original tree. Solution First, to do it in recursive method, we can perform pre-order traversal as usual, see preference….

## Interview Practice 09 – Verify Post-order Sequence of BST

Construct an algorithm to verify if a set of numbers is the post-order search result of a binary search tree.