---
# System prepended metadata

title: Zi Yuan

---


# Zi Yuan

# Question 1
```
Given an array of random positive numbers, move all the zeroes to the left of the array. 

Conditions: 
1) has to be done in place ( space-constrained, ie. cannot allocate a new array)
2) the order of non-zero elements on the right do not matter

Input :  arr[] = {0, 0, i>2, 5, 0, j>3, 1, 3}; 
Output : arr[] = {0, 0, 0, 2, 1, 3, 3, 5};
```

```javascript / ruby / go / python
// Write your solution here


// two  pointer i j 
// i, arr.length -1 
// while i <= j 

const moveZeroesInArray = (arr)=> {
    let i = 0
    let j = arr.length -1
    
    while(i <= j) {
        if(arr[i] === 0) {
            i += 1
        } else if(arr[j] === 0) {
            [arr[i], arr[j]] = [arr[j], arr[i]]
            j -= 1
        } else {
            j -= 1
        }
    }
    return arr
}



```

# Question 2
```
//        1 <---- root
//     /     \
//    2         3  --->
//   / \       / \
//  4   5     6   7  <-----\
// 89  1011  1213 1415
//
// 1) create the node type
// 2) write a function to print tree by level 
// ie. print "1","2","3","4","5","6","7"....
```



```javascript / ruby / go / python
// Write your solution here


const printTreeByLevel = (root) => {
    if(root === null ) return
    const queue = [root]
    while(queue.length > 0) {
        const current = queue.shift()
        
        console.log(current.value)
        
        if(current.left) queue.push(current.left)
        if(current.right) queue.push(current.right)
    }
}


// ie. print "1","3","2","4","5","6","7", "15", "14", "13", "12"....
const printTreeBySnakeLevel = (root) => {
    let queue = [root]
    let arr =[]
    let dirLeft = true
    
     
    while(queue.length) {
        let row = []
        
        for(let i = 0; i < queue.length; i++) {
            
        }
    }
    return arr
}














```

