Interview Question


Country: United States




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

int[] number =
		{ 8, 9, 9, 9, 9, 9, 9, 9, 9 };
		int length = number.length;
		boolean done = false;
		for (int i = length - 1; i >= 0; i--)
		{
			if (number[i] == 9)
			{
				number[i] = 0;
			}
			else
			{
				number[i] = number[i] + 1;
				done = true;
				break;
			}
		}
		if (!done)
		{
			int[] a = new int[length + 1];
			System.arraycopy(number, 0, a, 1, length);
			a[0] = 1;
			number = a;
		}

- Rohit Rajput January 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice! but fails for num = { -8, 9, 9}

Expected: - 8 9 8
Your program o/p: -7 9 9 

I know we need small fix to the code to get this correct but just thought I'd let you know that you had made an assumption that numbers are always positive.

- AmzFAILFacebookFailMSFTFail January 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Carol, you are right from some point of view, but should we consider a negative value anywhere in the array? I don't think so... But it is good to tell the interviewer that your assumption is that all digits are decimal digits - so 0<= number[i] <= 9

- Selmeczy, Péter January 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, make your assumptions explicit if you do not want to interviewer guessing what you thought about the problem

- AmzFAILFacebookFailMSFTFail January 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the first time I've seen an acronym for "write a code". It's particularly interesting because "write a code" is grammatically incorrect: it should be "write code". I generally hear a lot of people from India talk about "writing a code". Any particular reason this is so? Is that what they call it in schools?

To a native English speaker, this expression actually sounds funny because in that context, "code" generally means something like a combination to open a lock or a bar code at a store. You want me to write a code? Okay: 33728930...

It's unlike me to make a post which is admittedly off-topic, but when I read the question, I just thought that since many people here are from India, I'd probably be doing someone a service by pointing out that in interviews and cover letters and everything else (at least in the US), it's "write code", not "write a code". Don't tell the guy interviewing you that your summer job involved "writing a code to allow users to access a website" (sounds like the website has a backdoor), or worse, "writing a code for encryption purposes" (sounds like you're designing an encoding scheme of some kind).

- eugene.yarovoi January 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have lot of Indian classmates at my school. I did talk to them about using these kind of acronyms in verbal conversation. One of the friend said, "We do not know touch typing ( that is, to type without seeing keyboard) and hence tend to cut off the words to reduce the pain of switching from monitor to keyboard sight" and other said the same reason that we often see in US too - " lot of SMSing and tweeting". They left me thinking but they had the point.

- AmzFAILFacebookFailMSFTFail January 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad guys ! It should have been WAP - write a program or WC.

And one more clarification , the Interviewer said there won't be any negative inputs.

- Gloryhunter January 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude , Din't I apologise in the above comment ? Also , on a side note , I read other comments , before saying something . Grow up !

- Gloryhunter January 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I wasn't trying to be rude, and I apologize if what I said could be interpreted that way. I just wanted to get to the bottom of this "write a code" expression because it's the most common grammatical error I see on this site. Don't worry about it: last thing I want is my off-topic post turning into a flame war.

- eugene.yarovoi January 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No problem mate. It's all good :)

- Gloryhunter January 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def increment_array(a):
  for i in xrange(len(a) - 1, 0, -1):
    if a[i] < 9:
      a[i] += 1
      return a
    else:
      a[i] = 0
  return [1] + [0] * len(a)

- Anonymous January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume we have positive value.

Arr = input and output array pointer,
n = input and output array length.

void IncreaseArray(int*& Arr, int& n)
{
	int Index;
	bool bQuit=false;

	// start from the end backwards to the beginning:

	for(Index=n-1;Index>0;Index--)
	{
		if(Arr[Index]<9)
		{
			Arr[Index]++; // we just increase the value and quit
			bQuit=true;
			break;
		}
		else // i.e. the digit == 9
		{
			Arr[Index]=0; // thanser to the next digit
		}
	} // now we are at index "0", if we went through all loop

	if(bQuit==true) return;

	// special case: the very first digit:

	if(Arr[Index]<9)
	{
		Arr[Index]++;
		return;
	}

	// if the array was "9999", we need to make it "10000":

	Arr[Index]=0;
	int* Arr2;
	Arr2 = new int[n+1];
	Arr2[0] = 1;
	memcpy(&Arr2[1], &Arr[0], n*sizeof(int));
	delete [] Arr; // actually, we should not do that, as Arr can be a pointer to a STATIC array!
	Arr = Arr2;
	n++;
}

- sergey.a.kabanov January 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

+ve no-
while(a[i]==9)
{a[i]=0;
i--;}
if(a[i]!=9)
a[i]+=1;
-ve no
while(a[i]==0)
{a[i]=9;
i--;}
if(a[i]!=0)
a[i]-=1}

- cooldj February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

+ve no-
while(a[i]==9)
{a[i]=0;
i--;}
if(a[i]!=9)
a[i]+=1;
-ve no
while(a[i]==0)
{a[i]=9;
i--;}
if(a[i]!=0)
a[i]-=1}

- cooldj February 28, 2012 | 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