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
require 'benchmark' | |
module BenchmarkTest | |
def self.find_pair_naive(list, sum) | |
list.each_with_index do |first, index_1| | |
list.each_with_index do |second, index_2| | |
next if index_1.eql? index_2 | |
if first + second == sum | |
return [index_1, index_2] |
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
# This problem was asked by Uber. | |
# Given an array of integers, return a new array such that each element | |
# at index i of the new array is the product of all the numbers in the original | |
# array except the one at i. Solve it without using division and in O(n) time. | |
# For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. | |
# If our input was [3, 2, 1], the expected output would be [2, 3, 6]. | |
def products_of_others(list) | |
right_products = Array.new(list.length) | |
left_products = Array.new(list.length) |
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
# This problem was asked by Google. | |
# | |
# Given a stack of N elements, interleave the first half of the stack | |
# with the second half reversed using only one other queue. This should be done in-place. | |
# | |
# Recall that you can only push or pop from a stack, and enqueue or dequeue from a queue. | |
def reverse_interleave(stack) | |
return stack if stack.length < 3 | |
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 level_order_traversal(node) | |
queue = [node] | |
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 in_order_traversal(node) | |
in_order_traversal node.left if node.left | |
puts node.value |
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 |
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 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
// 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
// iterators | |
const fib = { | |
[Symbol.iterator]() { | |
let pre = 0, cur = 1; | |
return { | |
next() { | |
[ pre, cur ] = [cur, pre + cur]; | |
return { | |
done: false, value: cur | |
}; |
NewerOlder