0% found this document useful (0 votes)
132 views3 pages

MS Questions IIT D

The document contains 9 questions related to algorithms and data structures. It then provides solutions for questions 4 and 8. Question 4 asks how to sort an array of 16,000 integers that can only be loaded 1,000 at a time into memory. The solution uses a bit array to track the presence of each integer, loads integers in batches to set the bits, then scans the bit array to write out the integers in sorted order. Question 8 asks for code to print all permutations of a string; the solution provided uses a recursive permutation function that swaps characters to generate all permutations.

Uploaded by

api-3708029
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views3 pages

MS Questions IIT D

The document contains 9 questions related to algorithms and data structures. It then provides solutions for questions 4 and 8. Question 4 asks how to sort an array of 16,000 integers that can only be loaded 1,000 at a time into memory. The solution uses a bit array to track the presence of each integer, loads integers in batches to set the bits, then scans the bit array to write out the integers in sorted order. Question 8 asks for code to print all permutations of a string; the solution provided uses a recursive permutation function that swaps characters to generate all permutations.

Uploaded by

api-3708029
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) Two linked lists intersect at one node (imagine Y shape)...

after
intersecting, remaining nodes are common to both the link lists. how
do u find the point of intersection

2) Given a BST, how do u check whether it is a valid BST

3) You have n machines, each having n integers. Now u have to find


median of these n^2 numbers, but u can load only 2n integers at a time
in memory

4) Given an array having 16000 unique integers, each lying within the
range 1<x<20000, how do u sort it. U can load only 1000 numbers at
a time in memory.

5) Remove alternate nodes from a link list

6) Write code for removing loop from a link list

7) You have a BST containing integers. now Given any two numbers x
and y, how do u find the common ancestor of nodes which have these
values in them. You are given pointet to root of the BST.

8) Code for printing all permutations of a string.

9) Code for reversing words of the string

There were many more...


Solution for Qn 4
4) Given an array having 16000 unique integers, each lying within the
range 1<x<20000, how do u sort it. U can load only 1000 numbers at a
time in memory.

Solution:

Have an array of 2500 bytes (memory equaivalent of 625 ints) - Each bit in
the array will show the presence/absence of a num betw 1-20,000. Lets call
this the "presence-bit-map".

Since you can store the presence of 8 nos in a byte, you need (20,000/8) =
2500 bytes.

Initialize all bytes in the presence-bit-map to 0, meaning 'absent'.

Iterate the large integer list(be it in file / mem) once. For every number
encountered, set the corresponding bit to 1(indicating present) in the
presence-bit-map.

Now, by scanning the presence-bit-map once, you can write the present
numbers in sorted order.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void swap(char* src, char* dst)
{
char ch = *dst;
*dst = *src;
*src = ch;
}
/* permute [set[begin], set[end]) */
int permute(char* set, int begin, int end)
{
int i;
int range = end - begin;
if (range == 1) {
printf("set: %s\n", set);
} else {
for(i=0; i<range; i++) {
swap(&set[begin],
&set[begin+i]);
permute(set, begin+1, end);
swap(&set[begin],
&set[begin+i]); /* set back */
}
}
return 0;
}
int main()
{
char str[] = "abcd";
permute(str, 0, strlen(str));
return 0;
}

You might also like