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
""" Given a singly linked list, how do you find if there is a loop? | |
assumptions: | |
- `head` can be None or the first node in a linked list | |
""" | |
def is_loop(head): | |
if head is None or head.next is None: | |
return false | |
else: | |
first, second = head, head.next |
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
let fib = { | |
[Symbol.iterator]() { | |
let pre = 0, cur = 1; | |
return { | |
next() { | |
[pre, cur] = [cur, pre + cur]; | |
return { done: false, value: cur } | |
} | |
} | |
} |
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
// Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? | |
// brute force: for each character, iterate over each character in word, if character is not self and is found, return false. time: O(n^2) where n is # of characters in word, space: O(1); | |
// first appraoch: for each character, look up visited hash table to see if it exists, if not add it to visited. if exists, return false. return true at end of loop. time: O(n), space: O(n); | |
let isUnique = (word) => { | |
let visited = {}; | |
for (var char of word) { | |
if (!!visited[char]) { | |
return false | |
} else { | |
visited[char] = true; |
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
// Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? | |
// brute force: for each character, iterate over each character in word, if character is not self and is found, return false. time: O(n^2) where n is # of characters in word, space: O(1); | |
// first appraoch: for each character, look up visited hash table to see if it exists, if not add it to visited. if exists, return false. return true at end of loop. time: O(n), space: O(n); | |
let isUnique = (word) => { | |
let visited = {}; | |
for (var char of word) { | |
if (!!visited[char]) { | |
return 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
// constants | |
const NAME = `Alex Park` | |
console.log(`const: ${NAME}`) | |
// block scope: var vs let | |
// best explanation so far: | |
// https://www.sitepoint.com/joys-block-scoping-es6/ | |
for (var i=0; i<3; i++) { | |
var j = i * i | |
} |
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
// iterators | |
const fib = { | |
[Symbol.iterator]() { | |
let pre = 0, cur = 1; | |
return { | |
next() { | |
[ pre, cur ] = [cur, pre + cur]; | |
return { | |
done: false, value: cur | |
}; |
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
// Set data structure | |
let s = new Set(); | |
s.add(1); | |
s.add(2); | |
s.add(3); | |
console.log(s.size); | |
console.log(s.has(2), s.has(10)); | |
for (let k of s.values()) { |
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
class Node | |
attr_accessor :value, :left, :right | |
def initialize(value) | |
@value = value | |
end | |
end | |
def is_height_balanced?(node) | |
return true if node.nil? |
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
class Node | |
attr_accessor :value, :left, :right | |
def initialize(value) | |
@value = value | |
end | |
end | |
def pre_order_traversal(node) | |
puts node.value | |
pre_order_traversal node.left if node.left |
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
class Node | |
attr_accessor :value, :left, :right | |
def initialize(value) | |
@value = value | |
end | |
end | |
def post_order_traversal(node) | |
post_order_traversal node.left if node.left | |
post_order_traversal node.right if node.right |
OlderNewer