Wednesday, June 7, 2017

Dependency Inversion Principle

Dependency Inversion Principle basically says that when a class A depends on class B, do not instantiate class B in class A. Instead create a interface I that class B can implement and make class A depend on the interface I. This way we can plug in any other classes that implement from interface I in class A.

Basically the code should depend upon abstractions. By depending upon abstractions, we are decoupling implementations from each other.

Wednesday, September 17, 2014

Apparantly :P

http://myqc.wordpress.com/2012/08/13/amazon-interview-questions-cheatsheet/

Wednesday, July 30, 2014

Matrix paths from start to end traversing unoccupied cells

public class CoOrd
{
int X;
int Y;
public CoOrd(int x, int y)
{
X = x;
Y = y;
}
}

public static ArrayList getAllPaths(int[][] matrix, CoOrd start, CoOrd end)
{
ArrayList paths = new ArrayList();
getPaths(matrix, start, end, paths);
return paths; //assumings callers check the return ArrayList length
}

public static void getPaths(int[][] matrix, CoOrd start, CoOrd end, ArrayList paths)
{
if(start.X == end.X && start.Y == end.Y) //return when start meets end
{
return;
}
if(matrix[Start.X, Start.Y] == 0) //assuming 0 is an unoccupied cell and rest are occupied
{
paths.add(start);
}
else
{
/recurse right
CoOrd X1point = new CoOrd(start.X+1,start.Y);
getPaths(matrix, X1point, end, paths);

//recurse up
CoOrd Y1point = new CoOrd(start.X,start.Y+1);
getPaths(matrix, Y1point, end, paths);
}
return;
}

Adding 3 linked lists in the correct order and returning result linked list

#define E_INVALID_PARAMETER -1

int getNumber(node* root)
{
if(root == NULL)
{
return E_INVALID_PARAMETER;
}

int ret = 0;
node* temp = root;
while(temp)
{
int data = temp->data;
//checks to find data is valid
ret = ret*10 + data;
temp = temp->next;
}
return ret;
}

node * createLL(int num)
{
node* head = null;
node* prev = null;

while(num)
{
int data = num % 10;
head = (node *)malloc(sizeof(node));
if(head == NULL)
{
return NULL;
}
head->data = data;
head->next = prev;
prev = head;
num = num/10;
}

return head;
}

node * sumList (node* a, node *b, node* c)
{
if(a == NULL || b == NULL || c == NULL)
{
return NULL;
}
int aNum = 0;
int bNum = 0;
int cNum = 0;

aNum = getNumber(a);
bNum = getNumber(b);
cNum = getNumber(c);

if(a == E_INVALID_PARAMETER || b == E_INVALID_PARAMETER || c == E_INVALID_PARAMETER)
{
return NULL;
}

return createLL(aNum + bNum + cNum);
}

Wednesday, June 12, 2013

Insomnia

When thoughts suffocate your brain,
Brain unleashes the monster of imagination,
Monster spits the fire on dreams,
Dreams disclose their counterparts: nightmares,
nightmares spoil your sleep...

Sunday, June 9, 2013

Can't ask more from this weekend

Summer to Seattle is like Saturday of a week. The caffeinated blood of Seattlers craves for the Vitamin D from the Great Apollo. Some people make dedicated plans for Summer through out the year. Prices inflate, pockets deflate.

As you all know, my plans are sporadic. It all started off with an email thread to go to Northern Cascades. As usual, I was excited. No replies, only pages (Amazon folks will understand this). I called every one Friday to finalize the plan. Some didn't pick up and those who did wanted me to confirm from the one who didn't :(. I wanted to break this deadlock. Had it been code, I would have used mutex. But this is more complicated than that. I came who with a hope, the I will be showered with call from those who missed. When the clock ticked 12:00AM, I lost hope.

I called my colleague who invited me to join him for the active volcano trip. I was not interested because of the plans i made with other friends. Mt. Rainier!!! I am live 100 miles away from, but never got chance to go there. I was scared that one day, its going to explode and consume only me and ignorant folks like me. He was more than happy to welcome me, but time we are supposed to start had to be digested as a sour pill. 8:00 AM in the morning. I had to say YES!!! camouflaging the laziness in me.

Woke up at 7:15 and was ready by 7:55. Waiting for them to call. 8:30, no call. I didn't call them as i felt it will be like pressurizing them. Finally my Nokia sings my favorite ring tone at 9:30. He comes with a car loaded with 4 people. i scooped in with greeting other 3 with "I am Pavan" individually. I know, it sucks addressing your self to every others individually, but we are surrounded with these social etiquette which is difficult to dodge.

Yanni and Pink Floyd dominated air in the car. Finally reached the great Rainier. Make sure you stop at the first parking lot and take some pictures at the vista point there. Do not even think about using the restroom over there. Great view of the Rainier with river flowing through your legs waters your eyes with happiness. It was paradise. Trust me.

Then we did a small trek at Narada falls on a dead river with small streams of river trying to sneak away without notice. Then the final destination was Paradise point. The highest point that can be reached by a automobile. Enjoyed the scenery and headed back, but thought we should stop at one more visitor point called the reflection lake. It was frozen, but when the water melts, one can see Mt. Rainier's reflection on it. I closed my eyes, imagined it and rolled back to our car.

Feasting eyes, doesn't fill your stomach. So, had to stop at "Naan and curry". Visitors to Seattle, make sure you go to this place. Non veterinarians, Chicken Nihari is your dish. After stuffing esophagus, it was time for some kaju kulfi. It was the best kulfi i ever had.

Back to home. Thoughts of sleep conquered my thoughts. But friends gave a surprise visit. They wanted me to come with them to Seattle and chill out. 7 of us, rolled ourself to Kells on bell town in downtown. Stick of humans filled my nose as soon as i entered. Though the night is going to be a disaster. It was too hot inside and we all filled our water glasses with our sweat. Then comes the fun part. Live music!!! Couldn't ask for more. Stayed there till 2 AM. Chatted outside till 3 AM. Headed back to house and chatted there for 2 hours and slept.

Soo much happened in one day. More has happened, but i am bored of typing. Please wish me to have weekends like this forever :)