Skip to content

Instantly share code, notes, and snippets.

View Se7soz's full-sized avatar

Hussein Elsayed Se7soz

View GitHub Profile
@Se7soz
Se7soz / prob169.cpp
Created February 4, 2014 18:04
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
void maxElements(int arr[], int sz, int cnt) {
if(cnt > sz) return;
priority_queue<int, vector<int>, std::greater<int> > q;
for(int i = 0; i < sz; i++) {
q.push(arr[i]);
if(q.size() > cnt) q.pop();
}
@Se7soz
Se7soz / prob167.cpp
Created February 4, 2014 18:02
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
void recur_print(tree* root) {
if(root == NULL) return;
recur_print(root->lft);
cout << root->d << " ";
recur_print(root->rt);
}
void iter_print(tree* root) {
if(root == NULL) return;
@Se7soz
Se7soz / prob166.cpp
Last active August 29, 2015 13:56
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
int fairCoinToss() {
// returns either 0 for head, 1 for tail with prob 1/2 for each
}
// Return a value from 1 to n, with probability 1/n for each
int random_number(int n) {
int limit = 1;
int ret = 0;
@Se7soz
Se7soz / prob165.cpp
Created February 4, 2014 18:01
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
vector<vector<int> > subsets(int a, int b, int n) {
vector<vector<int> > ret;
if(b < a || (b-a+1) < n) return ret;
vector<vector<int> > s1 = subsets(a+1, b, n);
vector<vector<int> > s2 = subsets(a+1, b, n-1);
for(int i = 0; i < s2.size(); i++)
if(s2[i].size() == n-1) s2[i].insert(s2[i].begin(), a);
@Se7soz
Se7soz / prob164.cpp
Created February 4, 2014 17:59
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
void recur_print(tree* root) {
if(root == NULL) return;
recur_print(root->lft);
cout << root->d << " ";
recur_print(root->rt);
}
void iter_print(tree* root) {
if(root == NULL) return;
@Se7soz
Se7soz / prob163.cpp
Last active August 29, 2015 13:56
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
class Solution {
public:
int divide(int dividend, int divisor) {
int sign = 1;
if(dividend < 0) sign *= -1;
if(divisor < 0) sign *= -1;
return sign*divideHelper(dividend < 0 ? dividend*-1 : dividend, divisor < 0 ? divisor*-1 : divisor);
@Se7soz
Se7soz / prob162.cpp
Created February 4, 2014 17:58
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
node* recur_reverse(node* root) {
if(root == NULL) return root;
if(root->nxt == NULL) return root;
node* nd = recur_reverse(root->nxt);
node* lnk = root->nxt;
lnk->nxt = root;
root->nxt = NULL;
@Se7soz
Se7soz / prob161.cpp
Created February 4, 2014 17:57
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
void recur_print(tree* root) {
if(root == NULL) return;
recur_print(root->lft);
cout << root->d << " ";
recur_print(root->rt);
}
void iter_print(tree* root) {
if(root == NULL) return;
double power(double d, int n) {
if(n == 0) return 1;
int sign = n < 0 ? -1 : 1;
n *= sign;
double a = power(d, n/2);
if(n%2 == 0) a *= a;
else a *= a*d;
@Se7soz
Se7soz / prob159.cpp
Last active August 29, 2015 13:56
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
vector<string> dictionary;
int editDistance(const string& s1, const string& s2) {
int mem[s1.size()+1][s2.size()+1];
for(int i = 0; i <= s2.size(); i++)
mem[0][i] = i;
for(int j = 0; j <= s1.size(); j++)
mem[j][0] = j;