Google Interview Question for Software Engineers


Team: GFS
Country: India
Interview Type: Phone Interview




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

If I understand correctly, Bitmap class is supposed to provide a diff(Btimap other) method that should output the diff?

Also, is the goal to minimize number of changed/removed/added bits in the diff?
For example, given two bitmaps {0,1,0,1,0,1} and {1,0,1,0,1,0}, should the diff be <Added: {0}, Removed: {5}> or <Changed {0-5}>?

- blckembr November 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
using namespace std;

class ResizableBitmap
{
  int iWidth, iHeight;
  vector<unsigned char> vPixels;
  vector<bool> vMask;

  void ResizeVectors()
  {
	  vPixels.resize(3 * iWidth * iHeight);
	  vMask.resize(3 * iWidth * iHeight);
  }

public:

  ResizableBitmap()
{
	iWidth = 640;
	iHeight = 480;
	ResizeVectors();
}

  ResizableBitmap(int& iW, int& iH)
  {
	  iWidth = iW;
	  iHeight = iH;
	  ResizeVectors();
  }

  ~ResizableBitmap()
  {
	  vPixels.clear();
	  vMask.clear();
  }

  void CompareWithPreviousBitmap(const ResizableBitmap& PreviousBitmap);
  void SomeOperation();
};

void ResizableBitmap::CompareWithPreviousBitmap(const ResizableBitmap& PreviousBitmap)
{
	unsigned int i;

	// There can be 2 cases:
	// 1). Size is changed - the places that were added or substracted - are different
	// 2). Size is the same - each pixel is compared with the previous one by "XOR"
	if (PreviousBitmap.iWidth < iWidth)
	{
		// mark the new bitmap fragment as changed
	}

	if (PreviousBitmap.iHeight < iHeight)
	{
		// mark the new bitmap fragment as changed
	}

	if ((PreviousBitmap.iWidth == iWidth) && (PreviousBitmap.iHeight == iHeight))
	{
		for (i = 0; i < vPixels.size(); i++)
		{
			vMask[i] = (bool)(vPixels[i] ^ PreviousBitmap.vPixels[i]);
		}
	}
}

void ResizableBitmap::SomeOperation()
{
	unsigned int i;

	for (i = 0; i < vPixels.size(); i++)
	{
		if (vMask[i])
		{
			// do something only if the pixel is different than the previous one
		}
	}
}

- sergey.a.kabanov January 19, 2016 | 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