Morgan Stanley Interview Question for Interns


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 3 vote

1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20

We know that we will delete 5, 10 and 15 in this case. So, for the block from index 6-9 will be shifted left 1. block index 11-14 will shift over by 2. 16-19 will shift over by 3. Here we see a pattern. Because of continuous shifts, each block of numbers not divisible by 5 will shift left one more than the last block.

So, something like the following:

public void editBytes(byte[] arr) {
	

	int leftShiftAmount = 0;
	int i = 5;

	for (i = 5; i < arr.length; i+=5) {

		memmove(arr[i] - leftShiftAmount, arr[i + 1], sizeof(byte)*4);
		leftShiftAmount++;
	}

	int clearIndex = i - 5 - leftShiftAmount + 4;

	for (clearIndex = i - 5 - leftShiftAmount + 4; clearIndex < arr.length; clearIndex++) {//clear the garbage at the end, or you can just truncate up to this point


		arr[clearIndex] = 0;
	}

}

- SK April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with sk.
Just use FileChannel in Java. Truncate function is available.
Following is the example.

Initial file
1234 5678 1234 56
left-shift 6-9th character by 1 position
123456788 1234 56
left-shift 11-14th character by 2 position
12345678123434 56
left-shift 16-17th character by 3 position
12345678123456 56

The file has 17 bytes.17/5 is 3
Now truncate by 3

- s100banerjee April 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

fnDefrag(string file)
{
	using (var stream = File.Open(file, FileMode.Open))
	{
		byte[] barry=new byte[1];
		int current=0;
		int pivot=0;
		while(current<(stream.length-1))
		{
			stream.position=current;
			if((current+1)%5==0)
			{
				current++;
			}
			if(current!=pivot)
			{
				stream.read(barry,current,1);
				stream.write(barry,pivot,1);
			}
			pivot++;	
			current++;
		}
		stream.SetLength=pivot;
	}
}

- puncaz April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(int argc, char* argv[])
{
    FILE *fp_read, *fp_write;
    int counter = 1, i = 0;
    char ch;

    fp_read = fopen("input_file.txt", "r");
    fp_write = fopen("input_file.txt", "r+");

    if(!fp_read || !fp_write)
    {
        printf("File not present\n");
        exit(1);
    }

    fseek(fp_read, 0, SEEK_END);
    int size = ftell(fp_read);
    printf("Size = %d\n", size);
    fseek(fp_read, 0, SEEK_SET);

    while(counter < size)
    {
        ch = fgetc(fp_read);
        if(counter % 5 != 0)
        {
            fputc(ch, fp_write);
        }
        else
            i++;
        //printf("%c ", ch);
        counter++;
    }

    fclose(fp_read);
    fclose(fp_write);
    printf("Trucate by %d bytes", i);
    //Here truncate the file by "i" size
    return 0;
}

- Anand Barnwal April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void removeBytes(char *s, int interval) {
if (s==nil || interval<=0)
return;

//copy *p to *q if it is NOT on interval
//shift p until '\0'
char *q = s;
char *p = s;
int k = interval;
while (*p != '\0') {
k--;
if (k > 0 && p!=q) {
*q++ = *p++;
}
else if (k==0) {
p++;
k = interval;
}
}
*q = '\0';
}

- Victor April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is it bad to convert the byte array to ArrayList and the remove every char for multiple of 5 ?

- JRP June 08, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More