---
# System prepended metadata

title: Tecktok Quiz

---

# Tecktok Quiz
> Jiachen . 16 December 2020

### Deliverables and Breakdown

1. 30 Programing Questions
    - 10 Easy
    - 15 Medium
    - 5 Hard
2. 20 Frontend Questions
    - 5 Easy 
    - 10 Medium
    - 5 Hard
3. 20 Backend Questions
    - 5 Easy 
    - 10 Medium
    - 5 Hard

### Resources
1. [Backend engineering fundamental video](https://www.youtube.com/watch?v=V3ZPPPKEipA&t=106s)
2. [Triplebyte dump](https://github.com/cupanoticr/Triplebyte-Answers)
3. https://github.com/yangshun/front-end-interview-handbook

### Specifications
All code segments are written in 
 - Java 8
 - Python 3.5
 - Javascript ES6

### Rules
- no searching the internet
- no running code
- do remind them to have some paper and a pen close by (they'll need it!)

### Sample Question / Template

## Question 1 
Rating: Easy
Category: General

Question: **What day is it today?**
```javascript=
function getDay() {
    return "Wednesday";
}
```

Options:
1. Monday
2. Tuesday
3. Wednesday
4. Thursday

Answer: 3

Remarks: 

---
---

### Programming Quiz

## Question 1 
Rating: Easy
Category: Higher Order Functions

Question: **What day is it today?**
```javascript=
function getDay() {
    return "Wednesday";
}
```

Options:
1. Monday
2. Tuesday
3. Wednesday
4. Thursday

Answer: 3

Remarks: 

---

## Programming Quiz

### Easy Section (15 Questions)

### Question E1 
Rating: Easy
Category: Scoping

Question: **What is the result  of evaluating the following program?**
```javascript=
const x = 123;
function f(x){
    return x + 1;
}
f(1);
x;
```

Options:
1. 123
2. 1234
3. 2
4. 127

Answer: 1
Remarks: 

### Question E2
Rating: Easy
Category: Scoping

Question: **What is the result  of evaluating the following program?**
```javascript=
const x = 123;
function f(x){
    return x + x;
}
f(1);
```

Options:
1. 123
2. 1234
3. 246
4. 2

Answer: 4
Remarks: 


### Question E3
Rating: Easy
Category: General

Question: **Which of the following is an example of an intepreted language?**

Options:
1. Java 8
2. Java 13
3. Golang
4. PHP

Answer: 4
Remarks: 

### Question E4
Rating: Easy
Category: General

Question: **Which of the following is an example of a language that compiles to machine code?**

Options:
1. Java 8
2. Java 13
3. Golang
4. PHP

Answer: 3
Remarks: Java compiles to jvm bytecode!

### Question E5
Rating: Easy
Category: SWE

Question: **Which software engineering principle does this code segment violate?**
```java=
public int multiply(int x, int y) {
    return Math.log(x) % 1 == 0
        ? x << (int) Math.log(y)
        : x * y;
}
```
Options:
1. Practice KISSing
2. SLAP hard
3. Avoid Premature Optimizations
4. Avoid Magic Values

Answer: 3
Reference:
Code checks if op2 is a power of 2 and does bit shifting instead.
Its a compiler optimization for fast multiply! Full code segment below.

```java=
import java.lang.Math; 

class Main {  
  public static void main(String args[]) { 
    
    Main m = new Main();
    System.out.println(m.multiply(10, 4)); 
    
  } 
  
  public int multiply(int x, int y) {
        return Math.log(x) % 1 == 0
            ? x << (int) Math.log(y)
            : x * y;
  }
}
```

### Question E6
Rating: Easy
Category: SWE

Question: **Which software engineering principle does this code segment violate?**
```
readData();
salary = basic * rise + 1000;
tax = isTaxable ? salary * 0.07:0;
displayResult();
```
Options:
1. Practice KISSing
2. SLAP hard
3. Avoid Premature Optimizations
4. Avoid Deep Nesting

Answer: 3
Reference:
https://nus-cs2103-ay1819s1.github.io/cs2103-website/se-book-adapted/chapters/codeQuality.html#slap-hard

### Question E7
Rating: Easy
Category: SWE

Question: **Which software engineering principle does this code segment violate?**
```java=
public String getAddress() {
    if (this.address == null) {
        // Default Address
        return "ABC Avenue";
    }
    
    return this.address;
}
```
Options:
1. Practice KISSing
2. SLAP hard
3. Avoid Premature Optimizations
4. Avoid Magic Values

Answer: 4
Reference:
Should abtract it into a constant somewhere!
https://nus-cs2103-ay1819s1.github.io/cs2103-website/se-book-adapted/chapters/codeQuality.html#slap-hard


### Question E8
Rating: Easy
Category: Language

Question: **Which of the following is not considered to be an OO language?**

Options:
1. C#
2. C
3. Javascript (ES6)
5. Swift

Answer: 2
Reference:

### Question E9
Rating: Easy
Category: Language

Question: **You are trying to model a simple queue in a Sushi shop to know which customer to serve next. Which datastructure would seem the most appropriate?**

Options:
1. Array List
2. Linked List
3. Doubly Linked List
4. Skip List

Answer: 2
Reference:
You don't really need the reverse pointer for a simple queue.

### Question 10
Rating: Easy
Category: Programming

Question: **Which is the missing line in the code segment?**

```javascript=1

class Node {
    public int value;
    public Node next;
    
    public Node(int value) {
        this.value = value;
        this.next = null;
    }
}

// Get max element from linkedlist
// where linkedlist only contain non negative integers
class LinkedList {
    public int getMax(Node head) {
        return head == null
            ? -1
            // missing line
    }
}
```

Options:
1. : head
2. : getMax(head.value)
3. : getMax(head.next)
4. : Math.max(head.value, getMax(head.next));

Answer: 4
Reference:

---
---

### Medium Section (15 Questions)

### Question M1
Rating: Medium
Category: HOF

Question: **What is the result  of evaluating the following program?**
```javascript=
function f(z) {
    const x = z * 10;
    return y => z => z + y + x;
}
f(10)(20);
```

Options:
1. 123
2. 1234
3. 150
4. None of the above

Answer: Should observe via function type that a function is returned instead of a value.


### Question M2
Rating: Medium
Category: HOF

Question: **What is the result  of evaluating the following program?**
```javascript=
function f(z) {
    const x = z * 10;
    return y => z => z + y + x;
}
f(10)(20)(30);
```

Options:
1. 123
2. 1234
3. 150
4. 2

Answer: 3
Remarks: A medium / hard question. A little tricky if a student is not familiar with HOF and scoping rules for anon functions!

### Question M3
Rating: Medium
Category: -

Question: **This function returns the ___ of its 3 arguments**
```python=
def mystery(a, b, c): 
    if a > b:  
        if (b > c): 
            return b 
        elif (a > c): 
            return c 
        else: 
            return a  
    else: 
        if (a > c): 
            return a 
        elif (b > c): 
            return c 
        else: 
            return b  
```
Options:
1. Smallest
2. Middle
3. Largest
4. None of the above

Answer: 2
Reference:
https://www.geeksforgeeks.org/middle-of-three-using-minimum-comparisons/


### Question M4
Rating: Medium
Category: Sorting

Question: **What is the time complexity of this program?**
```python=
arr = [64, 25, 12, 22, 11] 
  
n = len(arr)

for i in range(n): 
    min_idx = i 
    for j in range(i+1, n): 
        if arr[min_idx] > arr[j]: 
            min_idx = j 
    
    tmp = arr[min_idx]
    arr[min_idx] = arr[i]
    arr[i] = tmp
    
print(arr)
```
Options:
1. O(n)
2. θ(n log(n))
3. O(n^2)
4. None of the above

Answer: 3
Reference:
Selection sort runs in worst case O(n^2)
https://www.geeksforgeeks.org/selection-sort/

### Question M5
Rating: Medium
Category: Sorting

Question: **What is the best case time complexity of this program?**
```python=
arr = [64, 25, 12, 22, 11] 
  
n = len(arr)

for i in range(n):
    swapped = False
    
    for j in range(0, n-i-1): 
        if arr[j] > arr[j+1] :
            tmp = arr[j]
            arr[j] = arr[j + 1]
            arr[j + 1] = tmp
            swapped = True
            
    if not swapped: 
        break
    
print(arr)
```
Options:
1. θ(n)
2. θ(n log(n))
3. θ(n^2)
4. None of the above

Answer: 1
Reference:
Potentially a hard questions, since most people are confused about upper lower bounds vs best / worst case.
In the best case (already sorted), bubble sort runs in theta n time
https://www.geeksforgeeks.org/selection-sort/

### Question M6
Rating: Medium
Category: Sorting

Question: **What is the name of this type of sort?**
```python=
def sort(arr): 
    for i in range(1, len(arr)): 
        key = arr[i] 
  
        j -= i
        while j >= 0 and key < arr[j]: 
            arr[j + 1] = arr[j] 
            j -= 1
        arr[j + 1] = key 
```
Options:
1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. None of the above

Answer: 3
Reference:
https://www.geeksforgeeks.org/insertion-sort/

### Question M7
Rating: Medium
Category: Sorting

Question: **What is the name of this type of sort?**
```python=
def sort(arr, low, high): 
    if low < high: 
        pi = partition(arr,low,high) 
        sort(arr, low, pi - 1) 
        sort(arr, pi + 1, high) 
```
Options:
1. Merge Sort
2. Bubble Sort
3. Quick Sort
4. Radix Sort

Answer: 3
Reference:
Partition function should be a big give away!
https://www.geeksforgeeks.org/insertion-sort/

### Question M8
Rating: Medium
Category: SWE

Question: **Which software engineering principle does this code segment violate?**
```java=
class Square extends Rectangle {
    public Square(int length) {
        super(length, length);
    }
}

class Rectangle {
    private int width;
    private int height;
    
    public Rectangle(int width, int height) {
        this.width  = width;
        this.height = height;
    }
}
```
Options:
1. KISS
2. LSP
3. SLAP
4. Reusability

Answer: 2
Reference:
Don't extend edge cases. Classic example of LSP.



### Question M9
Rating: Medium
Category: OOP

Question: **What will be the result of this running the main method?**
```java=class Main {  
  public static void main(String args[]) { 
    Circle c1 = new Circle(new Point(1, 2), 3);
    Circle c2 = new Circle(new Point(1, 2), 3);
    
    System.out.println(c1.equals(c2));
  } 
}

class Circle {
    private Point point;
    private int radius;
    
    
    public Circle(Point point, int radius) {
        this.point = point;
        this.radius = radius;
    }
    
}

class Point {
    private int x;
    private int y;
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
```
Options:
1. Compile Time Error
2. Runtime Error
3. True
4. False

Answer: 4
Reference:
default Object.equals compares addresses not class attributes (need to be overridden for that behaviour!)

### Question M10
Rating: Medium
Category: PL

Question: **Imagine that this variant of python has TCO (Tail Call Optimization) implemented. What would be the worst case space complexity for this code segment?**

```python=
def fact(n):
    if n == 0:
        return 1
        
    return n * fact(n-1)
    
print(fact(10))
```

Options:
1. O(1)
2. O(n)
3. O(n log n)
4. O(n^2)

Answer: 2
Reference:
In its true form, this would not be tail call optimizable since its not an iterative process.


### Question M11
Rating: Medium
Category: PL

Question: **Imagine that this variant of python has TCO (Tail Call Optimization) implemented. What would be the worst case space complexity for this code segment?**

```python=
def fact(n):
    def helper(x, acc):
        if x == 0:
            return acc
        
        return helper(x - 1, x * acc)
        
    return helper(n, 1)
    
    
print(fact(10))
```

Options:
1. O(1)
2. O(n)
3. O(n log n)
4. O(n^2)

Answer: 2
Reference:
Now this would be tail call optimizable since it's an iterative process! Can be written in js as well!


### Question M12
Rating: Medium
Category: Algo

Question: **Given this code that generations permutations from a string / list of characters, which line is the correct missing line?**


```python=
def permutation(lst): 
    n = len(lst)
    
    if n == 0: 
        return [] 
  
    if n == 1: 
        return [lst] 

    perms = []
  
    for i, m in enumerate(lst): 
       wout = lst[:i] + lst[i + 1:] 
  
       for p in permutation(wout): 
           # missing line
    return perms
```

Options:
1. perms.append(p) 
2. perms.append([m] + p) 
3. perms.append(wout + p) 
4. perms.append([m] + wout) 

Answer: 2
Reference:
This one should be pretty clear!

### Question M13
Rating: Medium
Category: Code

Question: **What does this program evaluate to?**

```javascript=
let x = 0
let y = 0

function fun(n) {
    if (n <= 1) {
        return n
    } else {
        x = fun(n - 1)
        y = fun(n - 2)
        return x + y
    }
}

fun(12)
```

Options:
1. 1
2. 6
3. 21
4. 144

Answer: 2
Reference:
CS1101S RA2 19/20
Can easily be run in console tho... (if not it'll be hard)


### Question M14
Rating: Medium
Category: Code

Question: **In what order do numbers get printed out onto the screen?**

```python=
def fib(n):
    print(n)
    if n <= 1:
        return n
    else:
        return fib(n - 1) + fib(n - 2)
        
fib(4)
```

Options:
1. 4 3 2 1
2. 4 3 2 1 1 2 1
3. 4 3 2 1 1 0 1 0
4. 4 3 2 1 0 1 2 1 0

Answer: 4
Reference:
CS1101S RA2 19/20
Can easily be run in console tho... (if not it'll be hard)

### Question M15
Rating: Medium
Category: Code

Question: **What is the result of evaluating the following program?**

```javascript=
function f(g, x) {
    return g(g(x));
}

f(y => x + 2, 4)
```

Options:
1. 8
2. 6
3. 4
4. Error

Answer: 4
Reference:
CS1101S RA2 19/20
Can easily be run in console tho... (if not it'll be hard)


---
---

### Hard Questions (10 Questions)
### Question H1
Rating: Hard
Category: 

Question: **What is the result  of evaluating the following program?**
```javascript=
function twice(f) {
    return x => f(f(x));
}

function add1(x) {
    return x + 1;
}

twice(twice)(add1)(10);
```

Options:
1. 10
2. 14
3. 11
4. None of the above

Answer: 2
Remarks:
This is a fun one!
- twice(twice)(add1)(10);
- (f => x => f(f(f(f(x)))))(add1)(10);
- add1(add1(add1(add1(10))));

### Question H2
Rating: Hard
Category: 

Question: **This `hasThreeSum` function is supposed to return true if and only if number contains 3 elements that sum to 0. Is this implementation correct, if not which line is the error at?**

```python=
def hasThreeSum(nums):
        n = len(nums)
        
        if n < 3:
            return False
        
        nums.sort()
        
        for i in range(n - 2):
            lo, hi = i + 1, n - 1
            
            while lo < hi:
                acc = nums[lo] + nums[hi] - nums[i]
                
                if acc == 0:
                   return True
                elif acc > 0:
                    hi -= 1
                else:
                    lo += 1

        return False
        
hasThreeSum([0, -5, 5, 6, 2])

```
Options:
1. Line 10
2. Line 13
3. Line 17
4. There is no error

Answer: 2
Reference:

should be `acc = nums[lo] + nums[hi] - nums[i]``
https://leetcode.com/problems/3sum/solution/

### Question H3
Rating: Hard
Category: OOP

Question: **If you instantiate a new TA named alex and call `get Pay()` what value would be returned?`**
```java=
interface Person {
    public Integer getPay();
}

class Staff implements Person {
    public Integer getPay() {
        return 20;
    }
}

class Student implements Person {
    public Integer getPay() {
        return -10;
    }
}

class TA extends Staff, Student {
    
}
```
Options:
1. Compile time Error
2. Runtime Error
3. -10
4. 20

Answer: 1
Reference:
Diamond problem, if you just try running the code you'll get a compile time error. You cant instantiate multiple classes in java due to the diamond problem (its ambiguous which getPay will be called)! 
https://www.geeksforgeeks.org/multiple-inheritance-in-c/


### Question H4
Rating: Hard
Category: ASM

Question: **After running this ARM code segment, what is the value of the v2 register at breakpoint 1?**
```arm=
    mov v2, #0
    mov a1, #5
    mov v1, #0
    cmp a1, v2
    movgt v1, #1
    cmp v1, #1
    beq l1
    mov v2, #2
    b l2
l1:
    ldr v2, =#1
l2:
    # breakpoint 1
```


Options:
1. null
2. 0
3. 1
4. 2

Answer: 3
Reference:
Can be done with no arm experience. 50 percent chance once you realize its really either 1 or 2 haha

Jlite source:
```java=
class Main {
    Void main() {
        Int a;

        a = 0;
        if (a < 5) {
            a = 1;
        } else {
            a = 2;
        }
        println(a);
    }
}
```

### Question H5
Rating: Hard
Category: Code

Question: **What does this program evaluate to?**

```python=
def mystery(A):
    n = len(A)
    i = n - 1
    while i >= 1:
        j = 1
        while j <= i:
            if A[j - 1] > A[j]:
                tmp = A[j - 1]
                A[j - 1] = A[j]
                A[j] = tmp
            j += 1
        i -= 1

arr = [4, 6, 1, 3, 8, 9, 2, 5, 7]
mystery(arr)
arr
```
Options:
1. [1, 2, 3, 4, 5, 6, 7, 8, 9]
2. [9, 8, 7, 6, 5, 4, 3, 2, 1]
3. [4, 6, 1, 3, 8, 9, 2, 5, 7]
4. [7, 5, 2, 9, 8, 3, 1, 6, 4]

Answer: 1
Reference:
This program sorts the arr in place. 
adapted from CS1101S RA2 19/20
