# COURSE â€“ CODING INTERVIEW PREPARATION QUIZ ANSWERS

## Week 3: Introduction to Algorithms

## Meta Front-End/Android Developer Professional Certificate

## Complete Coursera Answers & Study Guide

Click to Enroll in Coursera Meta Front-End Professional Certificate

## Introduction to Algorithms INTRODUCTION

As part of the Introduction to Algorithms module in the **Meta Front-End Developer Professional Certificate** from Coursera, you will gain the skills needed to approach data structure and algorithm problems.

This module covers the fundamentals of sorting and searching algorithms, including time and space complexity considerations. You will then learn how to utilize algorithmic approaches such as divide and conquer greedy algorithms, and dynamic programming when problem-solving. With this knowledge, you will be able to write efficient code for a variety of applications.

Upon completing this module, you will have acquired a solid foundation in understanding algorithms applicable in many technology fields. Start your journey today!

## Learning Objectives

- Introduce the concept and usage of algorithms
- Demonstrate how to visualize an algorithm
- Combine new and previously learned coding patterns to solve problems

## KNOWLEDGE CHECK: SORTING AND SEARCHING

### 1. Given an array of 6 numbers -> 6, 8, 19, 48, 9, 90 and applying a selection sort.Â How many swaps must occur before the array is sorted?

- 2
**(CORRECT)** - 4
- 6Â

**Correct: Thatâ€™s correct. The array is mostly ordered so only have to swap 19 and 9; and then 48 and 19.**

### 2. Given an array of numbers and a target value, using a loop, what is the worst-case time complexity to check if the number is present in the array?

- O(n)
**(CORRECT)** - O(1)
- O(log n)Â

**Correct: Thatâ€™s correct. To determine if a value was there, using a loop would mean checking every element in the array. **

### 3. A binary search can only be performed on a sorted dataset.

- True
**(CORRECT)** - FalseÂ

**Correct: Thatâ€™s correct. The nature of binary search is that it checks if the value is higher or lower and removes everything beyond the point of that conditional statement.Â **

### 4. Given the following snippet of pseudocode:

```
array = []
n = 4
FOR i = 0 TO n:
FOR j = 0 TO n:
array.add(i*j)
What is the space complexity of this problem?
```

- O(log n)
- O(n)
- O(n^2)
**(CORRECT)**

**Correct: Thatâ€™s correct. As n is looped through twice, the number of computations will reflect n*n or n^2.Â **

### 5. What advantage is there to changing element location using an in-place swap?Â

- It reduces the amount of space taken by removing the need to create another variable in memory.
**(CORRECT)** - It reduces the time taken to complete an algorithm through lowering the time complexity.Â
- It is a memory feature that allows many variables to reference the same memory location.Â

**Correct: Thatâ€™s correct. In-place swapping is done to arrays in place of creating new ones and storing the sorted data there. It is a good process for reducing the space complexity of a solution. **

## KNOWLEDGE CHECK: WORKING WITH ALGORITHMS

### 1. What isÂ memoization?

- It is an example of divide and conquer.
- It is a process of retaining the results from a computation so that they can be reused rather than recalculating a result.
**(CORRECT)** - It is a practice of only computing what is in the cache in place of expensive memory calls.

**Correct: Thatâ€™s correct! It is the practice of retaining results to speed up subsequent computations.Â **

### 2. The practice of breaking a problem into a set of overlapping subproblems is referred to as:Â

- Dynamic programming
**(CORRECT)** - Divide and conquer
- Memoization

**Correct: Thatâ€™s correct. This is a practice that employs a divide and conquer approach to breaking problems down. When an overlapping pattern has been identified, this approach further utilizes memoization to compute solutions more quickly. **

### 3. Quicksort is an example of divide and conquer?

- True
**(CORRECT)** - False

**Correct: Thatâ€™s correct. The array is repeatedly broken into smaller components until the data is sorted.Â **

### 4. Examine the following problem:

### A bank robber has entered a bank vault and sees 3 stacks of precious bars:Â Gold, silver and platinum. The gold weighs 6kg and is valued at 60 dollars. The silver weighs 1 kg and is valued at 5 dollars. And the platinum weighs 10kg and is valued at 110 dollars. The robber can only carry 38kg.Â What is the optimal combination of items to take? Your solution is to fill the bag with as many platinum bars as possible before moving to the gold and then the silver. What type of approach best describes this solution?

- Graph approach
- Dynamic programmingÂ
- Greedy approach
**(CORRECT)**

**Correct: Thatâ€™s correct. This approach involves taking the immediate item with the highest value regardless of any other factors. **

### 5. Why is a base case crucial when designing recursive solutions?Â

- Without it the function would go on forever.
**(CORRECT)** - The algorithm needs to know the shape of the minimum case so it can model the solution from it.Â
- It is used to ensure that the input diminishes at each call.Â

**Correct: Thatâ€™s correct. The base case is the termination condition that ends the iterative nature of the function. **

## Coursera Meta Front-End Developer Professional Certificate Answers and Study Guide

**Liking our content? Then donâ€™t forget to ad us to your bookmarks so you can find us easily!**

Weekly Breakdown | Meta Study Guides | Back to Top

## MODULE QUIZ: INTRODUCTION TO ALGORITHMS

### 1. Insertion sort is an example of divide and conquer?

- TrueÂ
- False
**(CORRECT)**

**Correct: Thatâ€™s correct. Insertion sort processes each element in relation to its surrounding elements until the data is eventually sorted. **

### 2. Given an array of 6 numbers [6,8,19,48,9,90] and applying insertion sort, how many swaps must occur before the array is sorted?

- 4
- 2
**(CORRECT)** - 6Â

**Correct: Thatâ€™s correct. The array is mostly ordered so only have to swap 9 twice, first with 48, then with 19. **

### 3. What time complexity is required to do a linear search?

- O(1)
- O(n)
**(CORRECT)** - ((log (n))Â

**Correct: Thatâ€™s correct. A linear search requires that you do a search of every item. So it will take n (the number of items) time to search. **

### 4. Why do we need Big-O notation to evaluate our programs?

- Because sorting requires that things are moved around to save space.Â
- Because sorting is complicated, and we need a complicated metric.Â
- Â Because measuring time is relative to a personâ€™s computer, so a relative metric is required.
**(CORRECT)**

**Correct: Thatâ€™s correct. A relative metric is required to measure time.**

### 5. What is parallelization?

- It is about writing your code in one go.Â
- It is about calling functions repetitively until they have achieved a base case.Â
- It is about running code at the same time in threads or on separate computers.
**(CORRECT)**

**Correct: Thatâ€™s correct. You have successfully identified a brief definition of parallelization. **

### 6. Why would you decide to use recursion?

- Recursion reduces the pressure on the compiler by making less stack calls.Â
- It looks cool and makes your code seem more intelligent.
- It lends itself well to a divide and conquer approach.
**(CORRECT)**

**Correct: Thatâ€™s correct. Recursion works well with the divide and conquer approach. **

### 7. Why does Memoization work well with dynamic programming?Â

- It takes up less space in the hard drive.Â
- It requires less compiling because it stores previous results, reducing the load on the CPU.
**(CORRECT)** - Because it takes a lot of memory to run some programs and memoization allows you to store data in smaller sizes.Â Â

**Correct: Thatâ€™s correct. Dynamic programming utilizes memoization because it stores the results of computations, meaning the computations donâ€™t have to be repeated. **

### 8. How are the principles of dynamic programming and greedy algorithms at odds with one another?Â

- The principle of dynamic programming is to exhaustively compute the best solution, while a greedy approach will favor take the immediate best option.
**(CORRECT)** - The greedy algorithm will use up CPU by monopolizing resources.Â
- Because dynamic programming will react with more agility to a program, while the greedy approach will be slower and more self-centered.Â

**Correct: Thatâ€™s correct. With dynamic programming, you can find the most best solution, whereas greedy algorithms have a specific process. **

### 9. Why is a binary search conducted in O(log n) time?

- Regardless of the size of the input, at every step the number of calculations is halved.
**(CORRECT)** - Because as it searches it sorts the elements.
- It is not, it is conducted in O(n).

**Correct: Thatâ€™s correct. Log n means that it is not instantaneous access but it rapidly reduces the lookup space. **

### 10.

```
def fibonacci (number)
if number < 2
number
else
fibonacci (number - 1) + fibonacci(number - 2)
end
end
```

### In the Fibonacci pseudocode above how many recursive instances can be seen?Â

- 0
- 1
- 2
**(CORRECT)**

**Correct: Thatâ€™s correct. The algorithm is being called on the last, and second to last number on the series. **

### 11. If provided with the following array of numbers and you applied the selection sort approach. How many swaps must occur before the array is sorted?Â

[9, 2, 4, 8, 7, 90]Â Â Â

- 3
**(CORRECT)** - 2
- 4

**Correct: Thatâ€™s correct. In this case 3 swaps occur to sort the array.Â Â Â Â Â Â Â **

### 12. If you have a data set and decide to use the linear search approach to locate an element, which of the below is accurate?Â Â Â Â

- Within the array of elements, the linear search will search from the start of the index until an appropriate element is found.
**(CORRECT)** - The linear search first checks the halfway point of the array of elements. Determining if the element is greater or smaller than the target element. Â Â Â

**Correct: Thatâ€™s correct! The linear search will search from the start of the index until an appropriate element is found, or there are no more elements to check.Â Â Â Â Â Â **

### 13. Which of the following options are benefits of the divide and conquer paradigm?Â Select all that apply:Â Â Â Â Â Â

- Â CombineÂ Â Â Â
- Memory managementÂ Â Â Â Â Â
- Â ParallelizationÂ
**(CORRECT)**

**Correct: Well done. Parallelism is when you have different threads or computers working on the same problem at the same time to complete it in a quicker time.Â Â Â Â Â **

### 14. In the following code, what is the base case?Â Â

```
def fibonacci(number)
if number < 2
number
else
fibonacci(number â€“ 1) + fibonacci(number â€“ 2)
end
end
```

- When the number >2Â Â Â Â Â Â
- When the number < 2
**(CORRECT)** - Fibonacci (number â€“ 1)Â Â Â Â Â Â

**Correct: Well done. That will cause the termination of the loop.Â Â Â Â Â Â **

### 15. Which of the steps are included in the dynamic programming process?Â Select all that apply:Â Â Â Â Â Â

- Alter the code to get to an outcome.Â Â Â Â Â Â
- Describe the optimum outcome.
**(CORRECT)** - Break the problem into smaller steps.
**(CORRECT)**

**Correct: Well done. When computing dynamic programming solutions, one must first determine the objective function.Â That is the description of what the optimum outcome is to be.Â Â Â Â Â Â Â **

**Correct: Well done.Â Breaking the problem into smaller steps can allow recursions to take place. That is when functions call themselves repeatedly until a solution is reached.Â Â Â Â Â Â Â **

### 16. Using the greedy algorithm, which route will be taken to move from node B â€“ D ?Â Select the correct option.

- B â€“ S â€“Â C â€“ E â€“ DÂ
**(CORRECT)** - B â€“ E â€“ DÂ Â Â Â Â Â
- B â€“ E â€“ H â€“ G â€“ F â€“DÂ Â Â Â Â

**Correct: Well done. That will be the route with the current most rewarding solution at each juncture.Â This approach only considers the next best choice.Â Â Â Â Â Â Â **

## Introduction to Algorithms CONCLUSION

Algorithms form an important part of computer science, and in this module you learned about some common sorting and searching algorithms as well as time and space complexity. You also explored how to work with algorithms, visualizing and problem solving with algorithmic approaches. Now that youâ€™ve completed this module, you have a strong foundation on which to build more complex skills.

**Coursera** offers many courses on computer science if youâ€™re interested in learning more. Join Coursera now!

#### Subscribe to our site

Get new content delivered directly to your inbox.

## Quiztudy Top Courses

## Popular in Coursera

- Meta Marketing Analytics Professional Certificate.
- Google Digital Marketing & E-commerce Professional Certificate.
- Google UX Design Professional Certificate.
- Meta Social Media Marketing Professional Certificate
- Google Project Management Professional Certificate
- Meta Front-End Developer Professional Certificate

**Liking our content? Then, donâ€™t forget to ad us to your BOOKMARKS so you can find us easily!**