Showing posts with label Algorithms amp; Data Structures. Show all posts
Showing posts with label Algorithms amp; Data Structures. Show all posts

Saturday, December 10, 2011

Getting a job in software development

 

There are lots of question-answer websites where people discuss their problems and try to find the possible solutions using the crowd (well known as crowdsourcing). Some of these sites are quora.com, chacha.com, stackoverflow.com, stackexchange.com and experts-exchange.com. Searching these sites for a specific topic, will bring you a lots of links which may not be relevant to your topic or questions you are looking for. Sometimes you may need to prepare the good list of articles related to a given topic using the best answered/discussed materials on these sites.

StackOverflow.com has done a really great job to prepare and present them in a good format. One of them is about Scala. You may check it out at: http://stackoverflow.com/tags/scala/info

 

While browsing through the HN, I encountered a post by WalterGR who have done a similar job for helping others.

This is a re-post of a reddit post for my users which was collected by WalterGR user.  While the job hunt, Walter had collected the reddit posts which he thought may be useful for others.

 

 

references:

http://hashfold.com/techfold/scala-the-programming-language-2/

http://www.reddit.com/r/cscareerquestions/comments/n5spv/getting_a_job_in_software_development_a_reddit/

 

 

 

Monday, October 31, 2011

Writing Queue Data Structure In C++

on the same line of our previous post, here is how to write Queue in c++:

[cpp]
<pre>
#include <iostream.h>

struct node
{
int data;
node *link;
};

class queue
{
private:
node *front, *rear;

public:

queue()
{
front = rear = NULL;
}

void addq(int item)
{
node *temp;

temp = new node;

if(temp == NULL)
cout << endl << "Queue is full";

temp->data = item;
temp->link = NULL;

if(front == NULL)
{
rear = front = temp;
return;
}

rear->link = temp;
rear = rear->link;
}

int delq()
{
if(front == NULL)
{
cout << endl << "Queue is empty";
return;
}

node *temp;
int item;

item = front->data;
temp = front;
front = front->link;
delete temp;
return item;
}

~queue()
{
if(front == NULL)
return;
node *temp;
while(front != NULL)
{
temp = front;
front = front->link;
delete temp;
}
}
};</pre>
[/cpp]

Friday, October 28, 2011

Writing Stack Data Structure in C++

on the same line of our previous post, here is how to write Stack in c++:

[cpp]
//stack program
#include <iostream.h>

struct node
{
int data;
node *link;
};

class stack
{
private:
node *top;

public:

stack()
{
top = NULL;
}

void push(int item)
{
node *temp;

temp = new node;
if(temp == NULL)
cout << endl << "Stack is full";

temp->data = item;
temp->link = top;
top = temp;
}

int pop()
{
if(top == NULL)
{
cout << endl << "Stack is empty";
return NULL;
}

node *temp;
int item;

temp = top;
item = temp->data;
top = top->link;
delete temp;
return item;
}

~stack()
{
if(top == NULL)
return;

node *temp;

while(top != NULL)
{
temp = top;
top = top->link;
delete temp;
}

}
};//end class stack

void main()
{
stack a;

s.push(4);
s.push(26);
s.push(12);
s.push(7);
s.push(19);
s.push(90);

int i = s.pop();
cout << endl << "Item popped = " << i;

i = s.pop();
cout << endl << "Item popped = " << i;

i = s.pop();
cout << endl << "Item popped = " << i;
}
[/cpp]

Wednesday, October 26, 2011

Writing Tree data structure in C++

I was browsing through the Facebook and encountered ProudEngineers page. The implementation provided there is awesome.
After going through it, I couldn't resist to mention on this blog so that our readers like you could also benefit from it.
Here is the code:
[cpp]
#include <iostream.h>

#define TRUE 1
#define FALSE 0

class tree
{

private:

struct node
{
node *l;
int data;
node *r;
}*p;

public:

tree();
void search(int n, int &found, node * &parent);
void insert(int n);
void traverse();
void in(node *q);
void pre(node *q);
void post(node *q);
int operator == (tree t);
int compare(node *pp, node *qq);
void operator = (tree t);
node * copy (node * q);
};

tree::tree()
{
p = NULL;
}

void tree::search(int n, int &found, node * &parent)
{
node *q;
found = FALSE;
parent = NULL;

if(p == NULL)
return;

q = p;
while(q != NULL)
{
if(q->data == n)
{
found = TRUE;
return;
}

if(q->data > n)
{
parent = q;
q = q->l;
}
else
{
parent = q;
q = q->r;
}

}
}

void tree::insert(int n)
{
int found;
node *t,*parent;

search(n, found, parent);

if(found == TRUE)
cout << endl << "such a node already exists";
else
{
t = new node;
t->data = n;
t->l = NULL;
t->r = NULL;

if(parent == NULL)
p = t;
else
parent->data > n?parent->l = t:parent->r = t;
}
}

void tree::traverse()
{
int choice;

cout << endl << "1. Inorder"
<< endl << "2. Preorder"
<< endl << "3. Postorder"
<< endl << "Your choice ";
cin >> choice;

switch(choice)
{
case 1:
in(p);
break;

case 2:
pre(p);
break;

case 3:
post(p);
break;
}

}

void tree::in(node *q)
{
if(q != NULL)
{
in(q->l);
cout << "\t" << q->data;
in(q->r);
}
}

void tree::pre(node *q)
{
if(q != NULL)
{
cout << "\t" << q->data;
pre(q->l);
pre(q->r);
}
}

void tree::post(node *q)
{
if(q != NULL)
{
post(q->l);
post(q->r);
cout << "\t" << q->data;
}
}

int tree::operator == (tree t)
{
int flag;

flag = compare(p, t.p);
return flag;
}

int tree::compare(node *pp, node *qq)
{
static int flag;

if((pp == NULL) && (qq == NULL))
flag = TRUE;
else
{
if((pp != NULL) && (qq != NULL))
{
if(pp->data != qq->data)
flag = FALSE;
else
{
compare(pp->l,qq->l);
compare(pp->r,qq->r);
}
}
}

return (flag);
}

void tree::operator = (tree t)
{
p = copy(t.p);
}

tree::node * tree:: copy(node *q)
{
node *t;

if(q != NULL)
{
t = new node;
t->data = q->data;
t->l = copy(q->l);
t->r = copy(q->r);
return (t);
}
else
return (NULL);
}

void main()
{
tree tt, ss;
int i, num;

for(i=0; i <= 6; i++)
{
cout << endl << "Enter data for the node to be inserted : ";
cin >> num;
tt.insert(num);
}

tt.traverse();
ss = tt;
ss.traverse();

if(ss == tt)
cout << endl << "Trees are equal";
else
cout << endl << "Trees are unequal";
}
[/cpp]

Thursday, October 6, 2011

Speeding up Fibonacci computation using Caching

Dynamic programming is essentially a tradeoff of space for time. Repeatedly re-computing a given quantity is harmless unless the time spent doing so becomes a bottleneck in the performance. In this case we should better be storing/caching the previously calculated values and looking them up when needed instead of re-computing.

Think about computing a Fibonacci number by recursion:

Fn = Fn-1 + Fn-2  (with F0 = 0, F1 = 1)

Lets explore the two different solutions. one without the caching and other with caching.

Example 1: code without the knowledge of the previous calculated values (caching/table lookup):

[cpp]

long Fib(int n)

{

if(n == 0) return 0;
if(n == 1 ) return 1;

return Fib(n-1) + Fib(n-2);

}

[/cpp]

Example 2: code with caching or table lookup.

In this example, we will first check if the Fib(n) already exists in the lookup table. If exists then we will use it else we will compute and store the computed result in the lookup table for future reference. Caches/Lookup table is useful here as the value 'Fib(n)' for a given number 'n' is always constant e.g. Fib(3) = 2 always.

[cpp]

#define UNKNOWN -1 /*for empty cell or item not found*/
long Cache[MAX]; /*we use table/array here however building
HashTable would be more efficient in space however arrays are
efficient in lookup time*/
long Fib(int n)
{
int i;

Cache[0] = 0;
Cache[1] = 1;

for(i=2; i< n; i++)  Cache[i] = UNKNOWN;

return FibCache(n);

}

long FibCache(int n)
{

if(Cache[n] == UNKNOWN)
Cache[n] = FibCache(n-1) + FinCache(n-2);

return Cache[n];

}

[/cpp]

Why Example 2 is faster than Example 1?

The first example always calculates Fib(i) whenever it encounters in the recursion cycle. e.g.  in calculation of Fib(6), we need to calculate Fib(5) and Fib(4). Fib(5) again needs to calculate Fib(4). So here we are calculating Fib(4) twice.

If you look at Example 2, we have a lookup array created which keeps track of previously calculated value at a specified index in the array. e.g. Fib(4) will be stored in Cache[4] with value 3. So when the program goes to calculate the Fib(5), it checks if Fib(4) is already exists in the lookup table. if yes then it uses and proceeds ahead without investing time in recalculating it.