About Me

My photo
Mostly software programming related blogs.

Monday, December 8, 2014

Given a binary tree, print it vertically. The following example illustrates vertical order traversal.
           1
        /    \
       2      3
      / \    / \
     4   5  6   7
             \   \
              8   9 

The output of print this tree vertically will be:
4
2
1 5 6
3 8
7
9


#include <iostream>
#include <vector>
#include <map>
#include <cstdlib>
using namespace std;

struct Node {
int key;
Node* left;
Node* right;
};

struct Node* newNode(int key)
{
    struct Node* node = (struct Node*) malloc(sizeof(struct Node));
    node->key = key;
    node->left = node->right = NULL;
    return node;
}

void getVerticalOrder(Node* root,int verticalLevel,map< int,vector<int> >& m) {
if(!root) return;
m[verticalLevel].push_back(root->key);
getVerticalOrder(root->left,verticalLevel-1,m);
getVerticalOrder(root->right,verticalLevel+1,m);
}

void printVerticalOrder(Node* root) {
map < int,vector<int> > m;
int verticalLevel = 0;
getVerticalOrder(root,verticalLevel,m);

map< int,vector<int> > :: iterator it;

for(it=m.begin();it!=m.end();it++) {
for(int i=0;i<it->second.size();++i) {
cout<<it->second[i]<<" ";
}
cout<<endl;
}
}

int main() {

Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->right->left->right = newNode(8);
    root->right->right->right = newNode(9);

    cout << "Vertical order traversal is \n";
    printVerticalOrder(root);

    return 0;
}

Thursday, September 11, 2014

Non programming questions

1) How smart pointer like provided by Boost work?
2) How memory leak and corruption is detected by tools like Valgrind?
3) How storage allocator like malloc works? Implement a basic one.
3) how deadlock can be detected? Then how to prevent it from happening?
4) How ping works? How telnet works? How SSH works? How FTP works? How http works? what is ICMP? What is NAT? What is PAT? DHCP servers. DNS servers.
5) Just on top level how virtualization softwares like VMware work?
6) Design questions. Like design a LIFT/elevator setup with multiple lifts
7) Setuid. setgid, chroot <--- Unix/linux
8) How Java virtual machine works? How garbage collector works?
9) Active Directory Server, LDAP etc
10) how facebook work? How facebook stores/loads data of a "timeline"? Considering more than billion FB users, billions of pics/videos etc stored in thousands of servers.
11) Memcached - open source caching system. Used by Facebook.


Wednesday, September 10, 2014

Given an integer find the immediate larger integer which is a palindrome

// #palindrome #C #Programming #String 

#include <stdio.h>
#include <string.h>

typedef unsigned int uint;

uint find_next_palin(uint inp);

main()
{
        uint num;
        printf("enter the number: ");
        scanf("%u", &num);

        uint ret = find_next_palin(num);

        printf("next palindrome is %u\n", ret);
}

uint find_next_palin(uint inp)
{
        char buff[32];
        uint ret, i1 =0, i2 =0, i = 0;

        sprintf(buff, "%d", inp);

        int len = strlen(buff);

        if (len == 1) {
                return inp;
        }

        if (len % 2 == 0) {
                i1 = buff[len/2 -1] - '0';
                i2 = buff[len/2] - '0';

                if (i1 < i2) {
                        buff[len/2 -1] = i1 + '0'+1;
                }
                for (i = 0; i < len/2; i++) {
                        buff[len/2+i] = buff[len/2-i-1];
                }
        } else {
                i1 = buff[len/2 -1] - '0';
                i2 = buff[len/2 +1] - '0';

                if (i1 < i2) {
                        buff[len/2 -1] = i1 + '0'+1;
                }
                for (i = 0; i < len/2; i++) {
                        buff[len/2+i+1] = buff[len/2-i-1];
                }
        }

        sscanf(buff, "%u", &ret);
        return ret;
}                                                                                                                                                                                  

Monday, September 8, 2014

meru genie cab/taxi guys highly unprofessional - review

My experience with them is highly disappointing. I had to go to railway station which is around 20KM from my house. I had booked the can in the afternoon for 6PM same day. As **they had accepted the booking** so cab should have come. But by 6'o clock no SMS or no call. Should not at least they inform the customers if due to some reason cab can't be arranged?

I called them at 6. They told that they are arranging and by 6:15 I will get the cab. Again no call or sms till 6.15. Again I called after 6:15, they said sir its not possible. I said at least you should have informed. He has no answer other that "sorry".

My personal advice don't have the option of this cab service. Very Very unprofessional.

#Bangalore #Cab #Taxi #Railway #Station #Airport #Meru #Genie #india #Review

Monday, July 14, 2014

Sanpdeal very unprofessional

I had ordered an item on snapdeal.com on 3rd july and as per them item would have been delivered by 7th july. Today its 10th july and still I have not seen the item. This is still ok. But the real issue is their customer care guys can’t tell me the status of the item. they just say its on transit. They also say they can’t cancel the item as item is on transit. how on earth they cant find where the item is. As per the policy I am reading as the item cost is less than 1000Rs I wont get the money refunded instead some snapdeal coupons. Full torturousexperience for me. They don’t have toll free number for customer care too. Also customer care guys are as good as nothing. They just read you rulebook that’s all. No help. Their email/facebook support doesn’t work too. You write n number of mails but no one replies.

I have been doing online shoping since last 10 years and have ordered 100s of items maily on ebay and flipkart and this is the worst exp. This was my first time on snapdeal and I tell everyone it is last time.

Read below FAQs on snapdeal. Just to tell these are plain lies.


I was never informed that there will be delay in delivery. Here in FAQs they write I will be informed about new date when item will be delivered. But even after 3 days forget email, even customer care guys can’t tell me when the item will be delivered.

here is the update on 14h July. What a unprofessional people they are.

horrible exp with snapdeal. Order something here first time with them and was told will be delivered on 7th. But was not delivered even on 10th. As I needed the time urgently so I wanted to cancel the order. They said item can’t be cancelled unless item is delivered back to them. Today on 14th also item is not delivered to me and when again I am requesting to cancel the delivery they say it cant be cancelled as its still in "transit". Even shamelessly they say they don’t know where the item is? I asked why dont you talk to your courier partner then they say as a customer I should talk to them.


=========================

#Snapdeal #Online #Buy #ebay #flipkart #review #Feedback #Delhi #Bangalore #India