You are on page 1of 6

Horoppa Hackathon-2012

Name : Surid Anirban Phone : 01812834831


Set-C

Ans to Ques 1: Sum up all the given numbers and subtract from 5050 to get the missing number. 5050 for the reason that 1+2+3+..+100=5050

Ans to Ques 4: For anagram test we can individually sort the letters of the two strings and then match to see whether anagram or not . because if the two strings are anagram they will look alike after the letters are sorted. bool isAnagram( string A,string B) { string tmp1,tmp2; tmp1= sort_letters_of(A); tmp2= sort_letters_of(B); if(tmp1==tmp2) return true; return false; }

Ans to Ques 7: To find the longest common prefix of a set of strings , we first make a TRIE data structure with those words and then we start traversing the trie starting from root until we find a node with multiple children. this works because we stop at the 1st occurrence of the node with multiple children . so upto that node there were no branching in the trie i.e, all the strings have that common path i.e common prefix.

Ans to Ques 9: Finding a value in binary search tree and returning the closest value if not found can be done in same function:Int find(node *p,int val)
{

If(p==null)return closest; // reached leaf and still not found diff=abs(p->data-val); If(diff==0)return val; //found If(diff<min) { Min=diff; closest=node->data; } iIf(p->data<val ) ans=Find(p->right,val); else ans=find(p-<left,val); Return closest;
}

In the class binary search tree we simply need to keep two variables closest and min to keep track of the closest value and the min difference so far. For searching val, instead of directly checking node->data==val , we are checking the difference is 0 or not so that we can also check whether the it is the closest so far or not.

Ans to Ques 8: To find the original ordering of the letters , we can advance recursively to set the individual ordering of two lettersafter the recursion is finished we have all the individual relations..so we can then make a topological sort to determine ultimate ordering. Letters are represented by nodes in a graph and relation between two nodes is determined asif A<B then an edge goes from A to B. i.e A should come first in topological order. Pseudocode: void set_edge(int strt , int end) { for(int i=strt+1;i<=end;i++) { if(word[i][pos]==word[i-1][pos])continue; graph[word[i-1][pos]].push_back(word[i][pos]); indegree[word[i][pos]]++; set_edge(strt,i); strt=i+1; } return ; } topological_sort();

if topological sort is not possible then there must be cycle. i.e the input sequence was contradictory

Ans to Ques 6: To remove duplicate words from a sentence, we just insert the words in a SET data structure and then extract the words one by one. Set will automatically eliminate duplicate ones.

Ans to Ques 5: To append a link list to its last in reverse order we can use a recursive function because when recursion will come back we will get the reverse order automatically on the way and we will call insert() right there. Void append(node *p)
{

If(p->nxt==null)return; // p->nxt ;so that last data is not append twice Append(p->nxt); Insert(node->data,h);
}

P.T.O

Ans to Ques 3: Cache I/O >memory I/O>network I/O>disk I/O


Here > means faster than. Cache i/o is the fastest no doubt as they are actually registers. Memory mapped i/o is the second fastest as in this system the CPU communicates with devices with the ADDRESS dedicated to that device i.e writes and reads at that address which is faster than disk and network i/o. Next comes Network i/o. Though it operates in electronic speed, various overheads make it slower than memory i/o. overheads are like transmission latency, common memory sharing of different pc etc. Disk i/o is the slowest as reading/writing in a disk involves mechanical speed which is much slower than electronic speed.

Ans to Ques 10: We assume that in a calculator only we can go from one button to its adjacent 4 buttons(left,right,up down).We can represent the buttons as node of a graph and set edges. then we can run a bfs to find the shortest path from one digit to another digit. We can also solve it mathematically treating the button as co-ordinate points
7=(2,0); 8=(2,1); 9=(2,2) 4=(1,0); 5=(1,1); 6=(1,2) 1=(0,0); 2=(0,1); 3=(0,2) 0=(-1,0)

Now the shortest distance between 2 button is just =abs(Xs-Xd)+abs(Ys-Yd)

P.T.O

Ans to Ques 2:
For a family tree we can use a vector of nodes where nodes are the info of

a person and looks like:

Struct node { Int fthr,mthr,id; //the index of fthr and mthr in the main vector Vector<int> spouse; //the index of spouse(s ) in the main vector Vector <int> child; //the index of children in the main vector Bool gender; //make the tree more precise }; Vector<node> people; If a person is born ,a new node will be inserted into the vector and his fthr,mthr will be updated. His id is the index in the vector. If a person gets married his spouse vector will be updated. If a person gets child his child vector will be updated. As internal reference of parents,spouses and children are kept so this is actually the vector representation of family tree. This data structure can handle unusual relationships between persons and divorce also..:P

End

You might also like