Skip to content

Instantly share code, notes, and snippets.

View isaiah-perumalla's full-sized avatar

isaiah isaiah-perumalla

View GitHub Profile
@isaiah-perumalla
isaiah-perumalla / x86mmodel.c
Created June 18, 2013 20:51
x86 memory model
#include <pthread.h>
#include <stdio.h>
int x, y = 0;
int r0, r1;
void *core0 (void *arg)
{
x = 1;
asm volatile ("" ::: "memory"); // ensure GCC compiler will not reorder
def solution(N, A, B, C):
edges = [(C[i],A[i],B[i]) for i in xrange(len(A))]
edges.sort(key=lambda e: e[0])
maxLenAt = [0]*N
costAt = [0]*N
def updatePath(u,costu,lenu, v,costv, lenv, cost):
if costu < cost and lenu+1 > lenv:
maxLenAt[v] = lenu+1
costAt[v] = cost
@isaiah-perumalla
isaiah-perumalla / twitter_water.py
Last active August 29, 2015 14:27
twitter water fill problem, solution to problem described here https://medium.com/@bearsandsharks/i-failed-a-twitter-interview-52062fbb534b
def water_collected(A):
sortedHeights = [(x,i) for i,x in enumerate(A)]
sortedHeights.sort(key=lambda v: v[0]*-1) #sort in decreasing order
leftIdx = rightIdx = sortedHeights[0][1]
waterLevel = 0
for i in xrange(1,len(A)):
x,idx = sortedHeights[i]
if idx > rightIdx:
water = (idx-rightIdx-1)*x
@isaiah-perumalla
isaiah-perumalla / 10131_bigger_smarter.cc
Created October 6, 2014 19:25
solution to ova problem 10131
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Elephant {
int iq, weight, index;
static bool iq_greater(const Elephant& e1, const Elephant& e2) {
return e1.iq >= e2.iq;
}
};
@isaiah-perumalla
isaiah-perumalla / Amazing-test-solution.md
Last active August 29, 2015 14:07
Solution to Amazing-test problem from hackerearth

#Problem

The essence of the problem is given a collection of n items of varying size, let Tsum be the sum of all item sizes. we need to able to answer the question, does a subset of items exist such that the sum of the items in the sumset is equal to K. where K is some integer that satisfies the constraints K <= Tmax and (total-K) <= Tmax . Where Tmax is the maximum time allowed.

#Solution

Brute force approach would be iterate through all the subsets of the items.

Let S1, S2 ... S2n all subsets, we iterate through each subset to see if there exist a subset Sk such that the sum of Sk = K. This approach is guarenteed to be correct, however it is impossibly slow because we have to iterate 2n subsets. since the number of items in the collection can reach 100, in the worst case we would have to examine 2100 subsets this is clearly intractable.