Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer Big O Notation Exercises #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Notes/Exercise/Questions/Exercise-04.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.**
**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.**
11 changes: 11 additions & 0 deletions Notes/Exercise/Solutions/Solution-01.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
**Use Big O Notation to describe the time complexity of the following function that determines whether a given yaer is a leap year:**

```js

function isLeapYear(year){
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}

````

The time complexity for `isLeapYear` is: `O(1)`. Regardless of the input the algorithm will still go through the same number of operations during operation.
16 changes: 16 additions & 0 deletions Notes/Exercise/Solutions/Solution-02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
**Use Big O Notation to describe the time complexity of the following function that sums up all the numbers from a given array:**

```js

const sumOfArray = (array) => {
let sum = 0;

for(let i = 0; i < array.length; i++){
sum += array[i];
}
return sum;
}

```

The time complexity for `sumOfArray` is: `O(n)`. The number of operations that will be used in the for loop will increase as the input increases.
3 changes: 3 additions & 0 deletions Notes/Exercise/Solutions/Solution-03.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Imagine you have a chessboard and put a single grain of rice on one square. On the second square, you put 2 grains of rice, since that is double the amount of rice on the previous square. On the third square, you put 4 grains and on the fourth square you put 8 grains. The pattern continues like that.**

**Write a function calculates which square you will need to place a certain number of rice grains. For example, for 16 grains, the function will return 5. Use Big O Notation to describe the time complexity of that function.**
8 changes: 8 additions & 0 deletions Notes/Exercise/Solutions/Solution-04.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.**

```js
const returnString = (arr) => {
return arr.filter(string => string.toLowerCase().startsWith('d'));
}
```
The above function will have a time complexity of `O(n)`. This is because the filteration operations will increase as the input increases.
16 changes: 16 additions & 0 deletions Notes/Exercise/Solutions/Solution-05.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
**The following function calculates the median from an ordered array. Describe its time complexity in terms of Big O Notation:**

```js

const median (array) => {
const middle = Math.floor(array.length / 2);

if(array.length % 2 === 0) {
return (array[middle - 1] + array[middle]) / 2;
}
return array[middle];
}

```

The time complexity for `median` is: `O(1)`. Regardless of the length of the input array accessing the middle elements will take constant time.