Skip to content

Instantly share code, notes, and snippets.

@itayB
itayB / findWaterInGlass.cpp
Created September 21, 2016 17:34
Program to find amount of water in a given glass
#include <iostream>
#include <string>
using namespace std;
int main() {
int input = 2;
int tmp;
for (tmp=1 ; tmp <= input ; tmp *= 2);
@itayB
itayB / lru.cpp
Created September 21, 2016 21:00
Implement LRU cache
#include <iostream>
#include <string>
#include <unordered_map>
#include <list>
using namespace std;
template <class K, class V> class LRU {
private:
/* Members */
@itayB
itayB / CrackingTheCodeInterview4.8.cpp
Last active September 23, 2016 07:04
First Common Ancestor
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
template <class T> class Node {
public:
#include <iostream>
#include <map>
using namespace std;
int fib(int n) {
static map<int,int> mem{{1,1}, {0,1}};
if (mem.count(n) > 0) {
@itayB
itayB / 3sum.cpp
Created September 25, 2016 18:48
Given array with integers, return 3 number with sum of zero.
// ref: https://en.wikipedia.org/wiki/3SUM
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
// Brute force solution O(n^3)
bool sum3A(int arr[], int n) {
@itayB
itayB / CrackingTheCodeInterview-16.8.cpp
Created September 26, 2016 15:03
English Int - Given any integer, print an English phrase that describes the integer
#include <iostream>
#include <map>
using namespace std;
static map<int, string> text {
{0, ""},
{1, "one"},
{2, "two"},
{3, "three"},
#include <iostream>
using namespace std;
int magicSearch(int arr[], int start, int end) {
if (start > end)
return -1;
int index = (start + end) / 2;
@itayB
itayB / snake.cpp
Last active September 27, 2016 18:18
Snake game - Object Oriented Design
#include <iostream>
#include <list>
#include <unistd.h>
using namespace std;
enum Move {
UP,
RIGHT,
DOWN,
@itayB
itayB / abstractFactory.cpp
Created September 27, 2016 19:04
Abstract Factory Example (Design Pattern)
#include <iostream>
using namespace std;
class Parts {
string specification;
public:
// Constructor
Parts(string specification) {
@itayB
itayB / singlton.cpp
Created September 27, 2016 19:39
Singlton Example (Design Pattern)
#include <iostream>
using namespace std;
class Singlton {
public:
static Singlton* getInstance() {
static Singlton* instance = nullptr;