## Morgan Stanley Interview Question for Interns

• 0

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;
}``````

}

Comment hidden because of low score. Click to expand.
0

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

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.write(barry,pivot,1);
}
pivot++;
current++;
}
stream.SetLength=pivot;
}
}``````

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

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

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

{
printf("File not present\n");
exit(1);
}

printf("Size = %d\n", size);

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

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

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';
}

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 ?

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.

### 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.