Interview Question
Country: United States
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.
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
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).
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.
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.
Dude , Din't I apologise in the above comment ? Also , on a side note , I read other comments , before saying something . Grow up !
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.
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++;
}
- Rohit Rajput January 06, 2012