The following exercises provide you with the opportunity to practice with Big O Notation. The solutions to these exercises are found in the section, ​ Chapter 3 ​ .

  1. Use Big O Notation to describe the time complexity of the following function that determines whether a given year is a leap year:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>function</strong> ​ isLeapYear(year) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr></tbody></table>

  2. Use Big O Notation to describe the time complexity of the following function that sums up all the numbers from a given array:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>function</strong> ​ arraySum(array) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>let</strong> ​ sum = 0;</td></tr><tr><td>​ <span>&nbsp;</span></td><td></td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>for</strong> ​(​ <strong>let</strong> ​ i = 0; i &lt; array.length; i++) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>sum += array[i];</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr><tr><td>​ <span>&nbsp;</span></td><td></td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ sum;</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr></tbody></table>

  3. The following function is based on the age-old analogy used to describe the power of compounding interest:

    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. On the fourth square, you put 8 grains, and on the fifth square, you put 16 grains, and so on.

    The following function calculates which square you’ll need to place a certain number of rice grains. For example, for 16 grains, the function will return 5, since you will place the 16 grains on the fifth square.

    Use Big O Notation to describe the time complexity of this function, which is below:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>function</strong> ​ chessboardSpace(numberOfGrains) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>let</strong> ​ chessboardSpaces = 1;</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>let</strong> ​ placedGrains = 1;</td></tr><tr><td>​ <span>&nbsp;</span></td><td></td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>while</strong> ​ (placedGrains &lt; numberOfGrains) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>placedGrains *= 2;</td></tr><tr><td>​ <span>&nbsp;</span></td><td>chessboardSpaces += 1;</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr><tr><td>​ <span>&nbsp;</span></td><td></td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ chessboardSpaces;</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr></tbody></table>

  4. The following function accepts an array of strings and returns a new array that only contains the strings that start with the character "a" . Use Big O Notation to describe the time complexity of the function:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>function</strong> ​ selectAStrings(array) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>let</strong> ​ newArray = [];</td></tr><tr><td>​ <span>&nbsp;</span></td><td></td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>for</strong> ​(​ <strong>let</strong> ​ i = 0; i &lt; array.length; i++) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>if</strong> ​ (array[i].startsWith(​ <em>"a"</em> ​)) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>newArray.push(array[i]);</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr><tr><td>​ <span>&nbsp;</span></td><td></td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ newArray;</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr></tbody></table>

  5. The following function calculates the median from an ordered array. Describe its time complexity in terms of Big O Notation:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>function</strong> ​ median(array) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>const</strong> ​ middle = Math.floor(array.length / 2);</td></tr><tr><td>​ <span>&nbsp;</span></td><td></td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <em>// If array has even amount of numbers:</em> ​</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>if</strong> ​ (array.length % 2 === 0) {</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ (array[middle - 1] + array[middle]) / 2;</td></tr><tr><td>​ <span>&nbsp;</span></td><td>} ​ <strong>else</strong> ​ { ​ <em>// If array has odd amount of numbers:</em> ​</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ array[middle];</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr><tr><td>​ <span>&nbsp;</span></td><td>}</td></tr></tbody></table>

Copyright © 2020, The Pragmatic Bookshelf.