This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Find the minimum value in an array of strings using a loop. For example, for this array: | |
a=['my','name','is','john','doe']; | |
**/ | |
const sortByLengthOnly = arr => arr.sort( (a, b) => a.length - b.length) | |
const sortByLengthAndValue = arr => arr.sort( (a, b) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- There is a `t` table with 3 fields: | |
-- `uid` - user ID | |
-- `dt` - date and time of message | |
-- `s` - text of the message | |
-- Write an SQL query to retrieve date and text of the last message for all users. | |
-- schema | |
CREATE TABLE `user_message_log` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"env": { | |
"browser": true, | |
"node": true, | |
"es6": true | |
}, | |
"plugins": ["react"], | |
"ecmaFeatures": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const maxProfit = arr => { | |
let bestBuyPrice = 1000000 | |
let maxProfit = -1 | |
for(let i = 0; i < arr.length - 1; i++) { | |
const currentBuyPrice = arr[i] | |
const nextSellPrice = arr[i+1] | |
if (currentBuyPrice < bestBuyPrice) bestBuyPrice = currentBuyPrice |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* n = 20 | |
* | |
* test merge: 0.517ms | |
* test bubble: 0.204ms | |
* | |
* n = 100 | |
* | |
* test merge: 3.295ms | |
* test bubble: 1.395ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mergeSort = arr => { | |
if (arr.length < 2) return arr | |
const middleIdx = Math.floor(arr.length / 2) | |
let left = arr.splice(0, middleIdx) | |
let right = arr.splice(0) | |
return merge(mergeSort(left), mergeSort(right)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bubbleSort = arr => { | |
const { length } = arr | |
for (let i = 0 ; i < length; i++) { | |
for (let j = 0; j < length - i - 1; j++) { | |
if (arr[j] > arr[j+1]) { | |
const temp = arr[j] | |
arr[j] = arr[j + 1] | |
arr[j+1] = temp | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sieveOfErathosphenes = num => { | |
const arr = [false, false] | |
for (let idx = 2; idx < num; idx ++) { | |
arr[idx] = true | |
} | |
for (let i = 2; i < Math.sqrt(num); i++) { | |
for (let j = 2; j * i <= num; j++) { | |
arr[i * j] = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const binarySearch = (arr, key) => { | |
const midIdx = Math.floor(arr.length / 2) | |
const midElement = arr[midIdx] | |
if (midElement === key) return true | |
if (arr.length === 1) return false | |
if (midElement > key) { | |
return binarySearch(arr.splice(0, midIdx), key) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fibonacci | |
* | |
* 1, 1, 2, 3, 5, 8, 13, 21, 34 | |
* | |
* fibonacci = O(n^2) - exponential | |
* fibMemo = O(n) - linear | |
*/ | |
/** O(n^2) **/ |