From 983cbf99dce45e60decf88afd4191d9baf2ff182 Mon Sep 17 00:00:00 2001 From: Peter Weinberg Date: Sun, 7 May 2017 22:20:42 -0400 Subject: [PATCH] fix(tests): improve sorting algos section --- .../coding-interview-algorithm-questions.json | 107 +++++++++--------- 1 file changed, 51 insertions(+), 56 deletions(-) diff --git a/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-algorithm-questions.json b/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-algorithm-questions.json index 7185ed141c9..bceca1d9836 100644 --- a/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-algorithm-questions.json +++ b/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-algorithm-questions.json @@ -383,7 +383,7 @@ "Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices.", "You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another element.", "For example pairwise([7, 9, 11, 13, 15], 20) returns 6. The pairs that sum to 20 are [7, 13] and [9, 11]. We can then write out the array with their indices and values.", - "
Index01234
Value79111315
", + "
Index01234
Value79111315
", "Below we'll take their corresponding indices and add them.", "7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Return 6", "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." @@ -426,7 +426,7 @@ "Dato un array arr, trova le coppie di elementi la cui somma è uguale al secondo argomento passato arg. Ritorna quindi la somma dei loro indici.", "Se ci sono più coppie possibili che hanno lo stesso valore numerico ma indici differenti, ritorna la somma degli indici minore. Una volta usato un elemento, lo stesso non può essere riutilizzato per essere accoppiato con un altro.", "Per esempio pairwise([7, 9, 11, 13, 15], 20) ritorna 6. Le coppia la cui somma è 20 sono [7, 13] e [9, 11]. Possiamo quindi osservare l'array con i loro indici e valori.", - "
Indice01234
Valore79111315
", + "
Indice01234
Valore79111315
", "Qui sotto prendiamo gli indici corrispondenti e li sommiamo.", "7 + 13 = 20 → Indici 0 + 3 = 3
9 + 11 = 20 → Indici 1 + 2 = 3
3 + 3 = 6 → Ritorna 6", "Ricorda di usare Leggi-Cerca-Chiedi se rimani bloccato. Prova a programmare in coppia. Scrivi il codice da te." @@ -438,7 +438,7 @@ "Dado uma matriz arr, encontre pares de elementos no qual a soma é igual ao segundo argumento arg e retorne a soma dos seus indices.", "Se multiplos pares tem o mesmo elemento numérico mas indices diferentes, retorne a soma dos menores indices. Uma vez que um elemento é usado, este não pode ser emparelhado novamente com outro elemento.", "Por exemplo pairwise([7, 9, 11, 13, 15], 20) retorna 6. Os pares que somam 20 são [7, 13] e [9, 11]. Nós podemos então criar a matriz com seus indices e valores.", - "
Index01234
Value79111315
", + "
Index01234
Value79111315
", "Abaixo nós pegamos os índices correspondentes e somá-los.", "7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Retorna 6", "Lembre-se de usar Ler-Procurar-Perguntar se você ficar preso. Tente programar em par. Escreva seu próprio código." @@ -453,22 +453,19 @@ "This is the first of several challenges on sorting algorithms. Given an array of unsorted items, we want to be able to return a sorted array. We will see several different methods to do this and learn some tradeoffs between these different approaches. While most modern languages have built-in sorting methods for operations like this, it is still important to understand some of the common basic approaches and learn how they can be implemented.", "Here we will see bubble sort. The bubble sort method starts at the beginning of an unsorted array and 'bubbles up' unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.", "This method requires multiple iterations through the array and for average and worst cases has quadratic time complexity. While simple, it is usually impractical in most situations.", - "Instructions: Write a function bubbleSort which takes an array of integers as input and returns an array of these integers in sorted order. We've provided a helper method to generate a random array of integer values." + "Instructions: Write a function bubbleSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.", + "Note:
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting alogrithm in action!" ], "challengeSeed": [ - "// helper function to generate a randomly filled array", - "var array = [];", - "(function createArray(size) {", - " array.push(+(Math.random() * 100).toFixed(0));", - " return (size > 1) ? createArray(size - 1) : undefined;", - "})(12);", - "", "function bubbleSort(array) {", " // change code below this line", "", " // change code above this line", " return array;", - "}" + "}", + "", + "// test array:", + "// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]" ], "tail": [ "function isSorted(arr) {", @@ -478,7 +475,9 @@ ], "tests": [ "assert(typeof bubbleSort == 'function', 'message: bubbleSort is a function.');", - "assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: bubbleSort returns a sorted array.');" + "assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: bubbleSort returns a sorted array (least to greatest).');", + "assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'message: bubbleSort returns an array that is unchanged except for order.');", + "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'message: bubbleSort should not use the built-in .sort() method.');" ], "type": "waypoint", "solutions": [], @@ -491,22 +490,19 @@ "title": "Implement Selection Sort", "description": [ "Here we will implement selection sort. Selection sort works by selecting the minimum value in a list and swapping it with the first value in the list. It then starts at the second position, selects the smallest value in the remaining list, and swaps it with the second element. It continues iterating through the list and swapping elements until it reaches the end of the list. Now the list is sorted. Selection sort has quadratic time complexity in all cases.", - "Instructions: Write a function selectionSort which takes an array of integers as input and returns an array of these integers in sorted order." + "Instructions: Write a function selectionSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.", + "Note:
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting alogrithm in action!" ], "challengeSeed": [ - "// helper function to generate a randomly filled array", - "var array = [];", - "(function createArray(size) {", - " array.push(+(Math.random() * 100).toFixed(0));", - " return (size > 1) ? createArray(size - 1) : undefined;", - "})(12);", - "", "function selectionSort(array) {", " // change code below this line", "", " // change code above this line", " return array;", - "}" + "}", + "", + "// test array:", + "// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]" ], "tail": [ "function isSorted(arr) {", @@ -516,7 +512,9 @@ ], "tests": [ "assert(typeof selectionSort == 'function', 'message: selectionSort is a function.');", - "assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: selectionSort returns a sorted array.');" + "assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: selectionSort returns a sorted array (least to greatest).');", + "assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'message: selectionSort returns an array that is unchanged except for order.');", + "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'message: selectionSort should not use the built-in .sort() method.');" ], "type": "waypoint", "solutions": [], @@ -529,22 +527,19 @@ "title": "Implement Insertion Sort", "description": [ "The next sorting method we'll look at is insertion sort. This method works by building up a sorted array at the beginning of the list. It begins the sorted array with the first element. Then it inspects the next element and swaps it backwards into the sorted array until it is in sorted position. It continues iterating through the list and swapping new items backwards into the sorted portion until it reaches the end. This algorithm has quadratic time complexity in the average and worst cases.", - "Instructions: Write a function insertionSort which takes an array of integers as input and returns an array of these integers in sorted order." + "Instructions: Write a function insertionSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.", + "Note:
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting alogrithm in action!" ], "challengeSeed": [ - "// helper function to generate a randomly filled array", - "var array = [];", - "(function createArray(size) {", - " array.push(+(Math.random() * 100).toFixed(0));", - " return (size > 1) ? createArray(size - 1) : undefined;", - "})(12);", - "", "function insertionSort(array) {", " // change code below this line", "", " // change code above this line", " return array;", - "}" + "}", + "", + "// test array:", + "// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]" ], "tail": [ "function isSorted(arr) {", @@ -554,7 +549,9 @@ ], "tests": [ "assert(typeof insertionSort == 'function', 'message: insertionSort is a function.');", - "assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: insertionSort returns a sorted array.');" + "assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: insertionSort returns a sorted array (least to greatest).');", + "assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'message: insertionSort returns an array that is unchanged except for order.');", + "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'message: insertionSort should not use the built-in .sort() method.');" ], "type": "waypoint", "solutions": [], @@ -568,26 +565,25 @@ "description": [ "Here we will move on to an intermediate sorting algorithm: quick sort. Quick sort is an efficient, recursive divide-and-conquer approach to sorting an array. In this method, a pivot value is chosen in the original array. The array is then partitioned into two subarrays of values less than and greater than the pivot value. We then combine the result of recursively calling the quick sort algorithm on both sub-arrays. This continues until the base case of an empty or single-item array is reached, which we return. The unwinding of the recursive calls return us the sorted array.", "Quick sort is a very efficient sorting method, providing O(nlog(n)) performance on average. It is also relatively easy to implement. These attributes make it a popular and useful sorting method.", - "Instructions: Write a function quickSort which takes an array of integers as input and returns an array of these integers in sorted order. While the choice of the pivot value is important, any pivot will do for our purposes here. For simplicity, the first or last element could be used." + "Instructions: Write a function quickSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. While the choice of the pivot value is important, any pivot will do for our purposes here. For simplicity, the first or last element could be used.", + "Note:
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting alogrithm in action!" ], "challengeSeed": [ - "// helper function generate a randomly filled array", - "var array = [];", - "(function createArray(size) {", - " array.push(+(Math.random() * 100).toFixed(0));", - " return (size > 1) ? createArray(size - 1) : undefined;", - "})(12);", - "", "function quickSort(array) {", " // change code below this line", "", " // change code above this line", " return array;", - "}" + "}", + "", + "// test array:", + "// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]" ], "tests": [ "assert(typeof quickSort == 'function', 'message: quickSort is a function.');", - "assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: quickSort returns a sorted array.');" + "assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: quickSort returns a sorted array (least to greatest).');", + "assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'message: quickSort returns an array that is unchanged except for order.');", + "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'message: quickSort should not use the built-in .sort() method.');" ], "tail": [ "function isSorted(arr) {", @@ -606,30 +602,29 @@ "title": "Implement Merge Sort", "description": [ "Another intermediate sorting algorithm that is very common is merge sort. Like quick sort, merge sort also uses a divide-and-conquer, recursive methodology to sort an array. It takes advantage of the fact that it is relatively easy to sort two arrays as long as each is sorted in the first place. But we'll start with only one array as input, so how do we get to two sorted arrays from that? Well, we can recursively divide the original input in two until we reach the base case of an array with one item. A single-item array is naturally sorted, so then we can start combining. This combination will unwind the recursive calls that split the original array, eventually producing a final sorted array of all the elements. The steps of merge sort, then, are:", - "1) Recursively split the input array in half until a sub-array with only one element is produced.", - "2) Merge each sorted sub-array together to produce the final sorted array.", + "1) Recursively split the input array in half until a sub-array with only one element is produced.", + "2) Merge each sorted sub-array together to produce the final sorted array.", "Merge sort is an efficient sorting method, with time complexity of O(nlog(n)). This algorithm is popular because it is performant and relatively easy to implement.", "As an aside, this will be the last sorting algorithm we cover here. However, later in the section on tree data structures we will describe heap sort, another efficient sorting method that requires a binary heap in its implementation.", - "Instructions: Write a function mergeSort which takes an array of integers as input and returns an array of these integers in sorted order. A good way to implement this is to write one function, for instance merge, which is responsible for merging two sorted arrays, and another function, for instance mergeSort, which is responsible for the recursion that produces single-item arrays to feed into merge. Good luck!" + "Instructions: Write a function mergeSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. A good way to implement this is to write one function, for instance merge, which is responsible for merging two sorted arrays, and another function, for instance mergeSort, which is responsible for the recursion that produces single-item arrays to feed into merge. Good luck!", + "Note:
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting alogrithm in action!" ], "challengeSeed": [ - "// helper function generate a randomly filled array", - "var array = [];", - "(function createArray(size) {", - " array.push(+(Math.random() * 100).toFixed(0));", - " return (size > 1) ? createArray(size - 1) : undefined;", - "})(25);", - "", "function mergeSort(array) {", " // change code below this line", "", " // change code above this line", " return array;", - "}" + "}", + "", + "// test array:", + "// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]" ], "tests": [ "assert(typeof mergeSort == 'function', 'message: mergeSort is a function.');", - "assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: mergeSort returns a sorted array.');" + "assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'message: mergeSort returns a sorted array (least to greatest).');", + "assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'message: mergeSort returns an array that is unchanged except for order.');", + "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'message: mergeSort should not use the built-in .sort() method.');" ], "tail": [ "function isSorted(arr) {", @@ -644,4 +639,4 @@ "releasedOn": "February 17, 2017" } ] -} \ No newline at end of file +}