Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

As n can be a number which might not be a multiple of 256, for the remaining r bytes to be read we can get it with one call to ReadFromDisk() and taking the first r bytes

def Read(n):
	t, r = n / 256, n % 256
	result = b""
	while t > 0:
		result += ReadFromDisk()
		t -= 1
	result += ReadFromDisk()[:r]
	return result

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

It's too simplified. What if there are two consecutive calls Read(1000) and Read(500). Since original API ReadFromDisk reads next 256 bytes, it's reasonable if Read(int N) behave the same way. In this case you need to track down a position in file and keep buffer that contains remaining of the last returned result.

- Alex M. June 03, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.practice.careercup;

import java.nio.Buffer;
import java.util.ArrayList;
import java.util.List;

public class ReadDataInBytes {

	
	public static BlockOfData readData(byte size){
		BlockOfData data = new BlockOfData();
		if(size > 0){
			while (size > data.size()){
				data.add(readFromDisk());
			}
			
			if(data.size() > size){
				data.trim(0, data.size()-size);
			}
		}
		return data;
	}
	
	
	public static class BlockOfData{
		
		List<Byte[]> data = new ArrayList<Byte[]>();
		
		byte size(){
			if(!data.isEmpty()){
				return (byte) (data.size() * data.get(0).length);
			}
			return 0;
		}
		
		void add(Byte[] e){
			data.add(e);
		}
		
		void trim(int indexStart, int indexEnd){
			if(!data.isEmpty()){
				
				int sizePerBlock = data.get(0).length;
				int numberOfBlocks = data.size();
				int indexNumberOfBlocks = numberOfBlocks - 1;
				int totalBytes = sizePerBlock * numberOfBlocks;
				if(indexStart > totalBytes){
					//wrong range supplied; 
					return;
				}
				
				int startIndexBlock = getIndexBlock(indexStart, sizePerBlock);;
				int endIndexBlock = getIndexBlock(indexEnd, sizePerBlock) - startIndexBlock;
				
				if(startIndexBlock > 0){
					for (int i = 0; i < startIndexBlock; i++) {
						data.remove(i);
						indexNumberOfBlocks--;
					}
					indexStart = indexStart - (startIndexBlock * sizePerBlock);
					//reset the index
					startIndexBlock = 0;
				}
				
				//index fits in this range of block now the values in this block to start index
				Byte[] arr = data.get(startIndexBlock);
				for (int j = 0; j < indexStart; j++) {
					arr[j] = Byte.MIN_VALUE;
				}
				
				if(endIndexBlock < indexNumberOfBlocks){
					for (int i = indexNumberOfBlocks; i > endIndexBlock; i--) {
						data.remove(i);
					}
					indexEnd = indexEnd - (endIndexBlock * sizePerBlock);
				}
				
				
				//index fits in this range of block now the values in this block to start index
				Byte[] arry = data.get(endIndexBlock);
				for (int j = indexEnd; j < sizePerBlock; j++) {
					arry[j] = Byte.MIN_VALUE;
				}
			}
		}

		protected int getIndexBlock(int index, int sizePerBlock) {
			int currentBlockSize = sizePerBlock; 
			int blockCount = 0;
			while(index >= currentBlockSize){
				currentBlockSize += sizePerBlock;
				blockCount++;
			}
			return blockCount;
		}
	}
	
}

- amandhanjal March 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what happens if you wish to read more data that is stored on your disk ?
will ReadFromDisk() just report some error or will it silently wrap around ?

- 111 May 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void copy(byte[] dst, int dstFrom, byte[] src, int n) {
	for (int s = 0, d=dstFrom; s < n; s++, d++) {
		dst[d] = src[s];
	}
}

byte[] Read(int sizeInBytes) {
	byte[] buffer = new byte[sizeInBytes];
	int read = 0, needToRead = sizeInBytes;
	byte[] block;
	while (needToRead >= 256) {
		block = ReadFromDisk();
		copy(buffer, read, block, 256);
		read += 256;
		needToRead -= 256;
	}
	if (needToRead>0) {
		block = ReadFromDisk();
		copy(buffer, read, block, needToRead);
	}
	return buffer;
}

- titok March 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void copy(byte[] dst, int dstFrom, byte[] src, int n) {
	for (int s = 0, d=dstFrom; s < n; s++, d++) {
		dst[d] = src[s];
	}
}

byte[] Read(int sizeInBytes) {
	byte[] buffer = new byte[sizeInBytes];
	int read = 0, needToRead = sizeInBytes;
	byte[] block;
	while (needToRead >= 256) {
		block = ReadFromDisk();
		copy(buffer, read, block, 256);
		read += 256;
		needToRead -= 256;
	}
	if (needToRead>0) {
		block = ReadFromDisk();
		copy(buffer, read, block, needToRead);
	}
	return buffer;
}

- titok March 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

try it in perl;

sub Read($x) {
my @final_data;
while ($x >=256) {
$x -= 256;
$temp_read=ReadFromDisk();
push @final_data, $temp_read;
}
my $temp_char=ReadFromDisk();
push @final_data, $temp_char[0..x-1];
return join("",@final_data);
}

- leo March 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code in java:

public byte[] readFromDisc(int size) {
  
  byte[] result = new byte[n];
  int i = 0;
  
  while (i<n) {
    byte[] buffer = readFromDisk();
    
    for(int j = 0; j < 256 && j < i%256; j++, i++)
      result[i] = buffer[j];
   
  }

  return result;
  
}

- cassiano May 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

test

- cassiano May 29, 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