There are 4 ways to do a depth first traversal, depends on how you would like to do it.

# Tag Archives: how

# How to Change the Computer Name in Ubuntu

1. Open a terminal window.

2. At the input prompt you will see this structure:

“`

nicholas@computer-name:~$ _

“`

3. So you have to edit the hostname file:

“`

sudo nano /etc/hostname

“`

4. When prompted, enter the administrator password and hit Enter.

5. The hostname file will open, showing the current computer name. Replace the name with the desired new name.

6. Hit Ctrl+X to save and exit.

7. New name will show when you open a new terminal window.

# No solution stacks listed in elastic beanstalk configuration

It is how you do the elastic beanstalk configurations.

eb init

You would be very frustrated when you can select no available solution stacks…

```
Select a solution stack.
Available solution stacks are:
Select (1 to 0):
```

It is because the credentials you use don’t have administrator rights. Go to Identity and Access Management (IAM) and add appropriate permissions to the user. =]

# 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 for this problem. One is with recursive function and another is with iterative function. For this problem, iterative function makes simpler answer. However, we have to learn using recursive function because in production code recursive function uses memory more efficiently while compared to iterative function.

### Sample

# node structure class Node: value = None left = None right = None # constructor def __init__(self, value, left=None, right=None): self.value = value self.left = left self.right = right # iterative way to solve this problem def iteratively_verify_universal_value_binary_tree(root): stack = [root] while len(stack) > 0: node = stack.pop() if node.value != root.value: return False if node.left: stack.append(node.left) if node.right: stack.append(node.right) return True # recursive method to solve this problem def recursively_verify_universal_value_binary_tree(node, root_value=None): if not node: return True if not root_value: root_value = node.value # set root value to compared with left_is_universal = recursively_verify_universal_value_binary_tree(node.left, root_value) # get left result right_is_universal = recursively_verify_universal_value_binary_tree(node.right, root_value) # get right result this_is_universal = node.value == root_value # get this result return left_is_universal and right_is_universal and this_is_universal # combine result # trees universal_value_tree = Node(1, Node(1, Node(1), Node(1)), Node(1, Node(1), Node(1))) non_universal_value_tree = Node(1, Node(1, Node(1), Node(2)), Node(1, Node(1), Node(1))) # testing print iteratively_verify_universal_value_binary_tree(universal_value_tree) # true print iteratively_verify_universal_value_binary_tree(non_universal_value_tree) # false print recursively_verify_universal_value_binary_tree(universal_value_tree) # true print recursively_verify_universal_value_binary_tree(non_universal_value_tree) # false

# 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 (5 cents), dime (10 cents) and quarter (25 cents). Coins with higher values are preferred in the change.

### Solution

Well this is actually a simple question, I could just fill the sum from the highest value coin to the lowest. However I chose to use a simplified version of knapsack algorithm. Whatever, they both works.

### Sample

# coin values coins = [25, 10, 5] # simple solution for this problem def simple_solution(sum): combination = [] for coin in coins: for i in xrange(sum / coin): combination.append(coin) sum %= coin return combination # result: [25, 25, 10, 5] print simple_solution(65)

# 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

# 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

To solve this, we can check each bit by shifting the bits one by one.

1. 1010 -> check 0

2. 101 -> check 1

3. 10 -> check 0

4. 1 -> check 1

So how to check it? we could use the AND function.

1. 1010 -> check 1010 & 1 = 0

2. 101 -> check 101 & 1 = 1 -> counter + 1

3. 10 -> check 10 & 1 = 0

4. 1 -> check 1 & 1 = 1 -> counter + 1

However, negative number will put this program into infinite loop. For example, shifting -10 (0xFFFFFFF6) several times gives 0xFFFFFFFF. Then shifting 0xFFFFFFFF will give 0xFFFFFFFF and makes infinite loops. Therefore instead of shifting the inputting number, we should shift the comparing number.

1. 1010 -> check 1010 & 1 = 0

2. 1010-> check 1010 & 10 = 1 -> counter + 1

3. 1010 -> check 1010 & 100 = 0

4. 1010 -> check 1010 & 1000 = 1 -> counter + 1