Skip to content

Instantly share code, notes, and snippets.

View h2rashee's full-sized avatar

Harris Rasheed h2rashee

View GitHub Profile
@h2rashee
h2rashee / word_chain.py
Created May 18, 2018 10:24
# Given a list of words, determine the length of the longest chain that can be formed by altering one character at a time
# TODO We can modularise this function better; too long
def longestChain(words):
# Initialise a hashtable mapping string length to the words
word_dict = {}
# List of word lengths in `words`
length_list = []
for word in words:
if len(word) not in length_list:
length_list.append(len(word))
@h2rashee
h2rashee / friend_circles.py
Created May 18, 2018 10:24
Given a set of relationships, determine the number of friend circles that exist
def friendCircles(friends):
# TODO: Could use a hashtable here to find if an element faster
seen = []
num_friend_circle = 0
# Let's look at each person's set of relationships
for i in xrange(0, len(friends)):
if i in seen:
continue
@h2rashee
h2rashee / primes.py
Created February 28, 2018 20:46
Find the first n primes
import math
primes = [2]
def get_primes(n):
# Basic input validation
assert n >= 0
# Let's check what we have memoised already
if len(primes) >= n:
@h2rashee
h2rashee / server.py
Created January 12, 2018 20:32
Process GET/PUT HTTP requests to hash/fetch messages
import ast
import hashlib
from flask import Flask, request
app = Flask(__name__)
"""
I assume this is a stateful service where GET requests are only
to be served when a POST has been done on that same message. The
"original message" reference is unclear.
@h2rashee
h2rashee / find-pair.py
Last active January 14, 2018 23:43
Given a list of products with prices, find two that add up closest (equal to or just below) the provided value
import collections
import sys
""" This program's BigO notation is O(n) """
""" We look at every element in the list at most once """
""" For the three person gift case, the BigO notation is O(n^2) """
def find_closest_pair(products, target):
@h2rashee
h2rashee / myprogram.py
Created January 12, 2018 20:29
Given a pattern, generate all combinations
import sys
""" This program's BigO notation is 2^X """
def find_combinations(string):
"""
Given a string with X as placeholders, replace them and find
all combinations with 0s and 1s
"""
@h2rashee
h2rashee / Solution.java
Last active January 10, 2018 20:46
Given a binary tree, determine the shallowest depth of the tree
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class TreeNode {
TreeNode left;
TreeNode right;
@h2rashee
h2rashee / garage_schedule.py
Created November 3, 2017 16:34
Determine the number of buses in a garage at any given time
# Design a garage
# Given a schedule for 24 hours
# Entry and exit schedule of the garage
# API, take a time and return number of buses in the garages
# Interface to define the schedule
# ID EnterTime ExitTime
# 1 00:30 02:30
@h2rashee
h2rashee / building_allocation.py
Created November 3, 2017 16:33
Given a list of requests, process them and return true if they are all successful; false otherwise
Q. Amazon has a portfolio with many buildings and warehouses that it manages for utility monitoring.
We have an api provided with a function: Future<AddBuildingResponse> addBuildingToPortfolio(Building building)
Implement a function that accepts a list of Buidlings and makes that call to add the buildings to our portfolio. This function should fail if any building fails to add, and return success only if all buildings are added successfully.
async getResponse (future) => // wait 3 s if response is present return, if not continue (gets nothing back)
You can additionally assume AddBuildingResponse contains a isSuccess flag to check for status
@coroutine
def addBuildingsToPortfolio(bldgs_requests):
'''
@h2rashee
h2rashee / blob_boundary.py
Created October 19, 2017 08:47
Given a 2D array (list of lists), print the extreme indices of the 1s (blob) in as few reads as possible
# A Blob is a shape in two-dimensional integer coordinate space where all cells have at least one
# adjoining cell to the right, left, top, or bottom that is also occupied. Given a 10x10 array of
# boolean values that represents a Blob uniformly selected at random from the set of all
# possible Blobs that could occupy that array, write a program that will determine the Blob
# boundaries. Optimize first for finding the correct result and then for minimizing the number of
# cell value reads, balancing minimization against elegance and clarity.
# Assumptions:
#