Skip to content

Instantly share code, notes, and snippets.

@adamlum
adamlum / CodingInterviewPractice_2013_01_02.cs
Created January 5, 2013 22:29
Implement a simple singly-linked list data structure. Write a function to remove a list's 3rd from last element. (Challenge: can you do it in a single list traversal?) Write a function to remove all duplicates from a linked list. (Challenge: can you do it without storing any extra data?) Write a function to detect a loop in a linked list.
/****************************************************************
Adam Lum
01/05/2013
Coding for Interviews on Linked Lists
http://codingforinterviews.com
Implement a simple singly-linked list data structure.
Write a function to remove a list's 3rd from last element.
@adamlum
adamlum / CodingInterviewPractice_2012_12_24.cs
Created December 28, 2012 21:03
Write a function that merges an array of already sorted arrays, producing one large, still sorted array. (included a C# integer array merge sort method)
/****************************************************************
Adam Lum
12/28/2012
Coding for Interviews on Merge Sort
http://codingforinterviews.com
Write a function that merges an array of already sorted arrays,
producing one large, still sorted array.
@adamlum
adamlum / CodingInterviewPractice_2012_12_17.cs
Last active December 9, 2015 19:59
Write a function that returns whether a given binary tree is a valid binary search tree.
/****************************************************************
Adam Lum
12/17/2012
Coding for Interviews on Binary Search Trees
http://codingforinterviews.com
Write a function that returns whether a given binary tree
is a valid binary search tree.
@adamlum
adamlum / transformer.rb
Created March 7, 2011 01:52
Adam Lum's solution to RPCFN #8 (XML Transformer)
# Adam Lum's solution to RPCFN #8
# http://rubylearning.com/blog/2010/04/07/rpcfn-xml-transformer-8/
require 'rubygems'
require 'hpricot'
require 'rexml/document'
include REXML
# Get the source file(s) via the command line argument, didn't see anything
# in the challenge instructions detailing this step.
@adamlum
adamlum / fair_distribution.rb
Created March 7, 2011 01:51
Adam Lum's solution to RPCFN #6 (Fair Distribution)
# Adam Lum's solution to RPCFN #6
# http://rubylearning.com/blog/2010/01/26/rpcfn-fair-distribution-6/
# Definitely not the most elegant, efficient, or Computer Science-y
# way to handle this challenge, but it's _a_ solution to these small sets.
# (For the sake of my code, hopefully the hypothetical t-shirt printing
# company in the problem never owns more than 4 machines) :)
class FairDistribution
@adamlum
adamlum / maze.rb
Created January 23, 2010 03:31
Adam Lum's solution to RPCFN #5 (Mazes)
# Adam Lum's solution to RPCFN #5
# http://rubylearning.com/blog/2009/12/27/rpcfn-mazes-5/
class Maze
attr_accessor :maze_array, :start_point, :end_point
def initialize(in_maze)
@start_point = []
@end_point = []
@maze_array = in_maze.split("\n")
(0..@maze_array.size - 1).each do |i|
@adamlum
adamlum / polynomial.rb
Created January 23, 2010 01:27
Adam Lum's solution to RPCFN #4 (Ruby**Fun)
# Adam Lum's solution to RPCFN #4
# http://rubylearning.com/blog/2009/11/26/rpcfn-rubyfun-4/
class Polynomial
attr_accessor :coefficient_array
def initialize(in_coefficient_array)
if (in_coefficient_array.size < 2)
raise ArgumentError, "Need at least 2 coefficients."
end
@coefficient_array = in_coefficient_array
@adamlum
adamlum / short_circuit.rb
Created January 22, 2010 23:55
Adam Lum's solution for RPCFN #3 (Short Circuit)
# Adam Lum's solution for RPCFN #3
# http://rubylearning.com/blog/2009/10/30/rpcfn-short-circuit-3/
INFINITY = 1 << 64
def dijkstra (graph, start, destination)
# Initialize
nodes = []
graph.each do |c|
nodes << c[0]
@adamlum
adamlum / average_time_of_day.rb
Created January 22, 2010 19:08
Adam Lum's solution to RPCFN #2 (Average Arrival Time for a Flight)
# Adam Lum's solution to RPCFN #2
# http://rubylearning.com/blog/2009/10/08/rpcfn-average-arrival-time-for-a-flight-2/
require 'time'
SECONDS_TO_CHECK_FOR = 10800 # Assuming 3 hours is a decent difference check
SECONDS_IN_DAY = 86400
def average_time_of_day(times)
count_greater = 0
@adamlum
adamlum / shift_subtitle.rb
Created January 21, 2010 21:05
Adam Lum's solution for RPCFN #1 (Shift Subtitle)
# Adam Lum's solution for RPCFN #1
# http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/
require 'optparse'
# Get the command line arguments
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: shift_subtitle.rb [options] input_file output_file"
opts.on("--operation [OPERATION]") do |o|