Interview Questions

Imagine you have a hard disk with blocks of memory 1..n.......

Software QA/Tests Interview Questions from Microsoft


(Continued from previous question...)

Imagine you have a hard disk with blocks of memory 1..n.......

Question:
Imagine you have a hard disk with blocks of memory 1..n. Files can be stored on one or more of these blocks. If a file spans more than one block, you can know the next block by querying the current block. Also you can query block with Node.isEmpty() method to know if it has any data. The system has a table which has a list of the files and the first node of each file. But that table has been corrupted, reconstruct that table. The names of the files are immaterial and you can get the index as well as the name of the nextblock when you query for Node.NextBlock()


maybe an answer:


public Dictionary<string,List<Block> > ReCreateTable(List<Block> hardDisk)
{
int[] flags = new int[hardDisk.Count];
Dictionary<string, List<Block>> table = new Dictionary<string, List<Block> >();
for(int i=0;i<hardDisk.Count;i++)
{
if(hardDisk[i].IsEmpty)
{
flags[i]=2;
continue;
}
//This is the bug I had during the actual coding
//else if(hardDisk[i].NextBlock=null)
//flags[i]=0;
else if(hardDisk[i].NextBlock!=null)
{
Block temp=hardDisk[i];
while(temp.NextBlock!=null)
{
int num= (int)Char.GetNumericValue(temp.NextBlock.Name[temp.NextBlock.Name.Length-1]);
flags[num]=1;
temp=temp.NextBlock;
}
}
}
int cnt = 1;
for (int i = 0; i < flags.Length; i++)
{
if(flags[i]==0)
{
Block b=hardDisk[i];
string filename = "File" + cnt.ToString();
//During the interview I was only returning the first block of a //file. This is a better solution.
List<Block> fp = new List<Block>();
fp.Add(b);
while (b.NextBlock != null)
{
fp.Add(b.NextBlock);
b = b.NextBlock;
}
table.Add(filename, fp);
cnt++;
}
}

return table;
}
}

(Continued on next question...)

Other Interview Questions