471,092 Members | 1,028 Online

# HCL interview questions can any one say solution for it

Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.

Sep 25 '06 #1
11 7948

sandeep wrote:
Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.
First the sum of the array is not 304 its 305.

Assuming that the sum would be 305, traverse through the array checking
for zero and return the address using the index. Its simple :)

-kondal

Sep 25 '06 #2

kondal wrote:
sandeep wrote:
Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.

First the sum of the array is not 304 its 305.

Assuming that the sum would be 305, traverse through the array checking
for zero and return the address using the index. Its simple :)

-kondal
You mean to say like polling method ( repeatedly checking full array
method ) but the interviewer told me to use other logic

Sep 25 '06 #3
sandeep wrote:
Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.
First, what is an HCL interview?

Second, in order for the sum to be 304, one of the elements would need
to be reduced by 1, not set to zero.

Finding an element with the value of zero would be easy, as would
finding its address, but I don't see what 304 has to do with anything.

Sep 25 '06 #4
jmcgill schrieb:
sandeep wrote:
>>Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.

First, what is an HCL interview?
An interview by the Holy C League. About as expected as the
Spanish Inquisition.

SCNR
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 25 '06 #5
Michael Mair said:
jmcgill schrieb:
>>
First, what is an HCL interview?

An interview by the Holy C League. About as expected as the
Spanish Inquisition.
But... but...?!?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 25 '06 #6
"sandeep" <sa*******@incore.inwrites:
Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.
Sorry, but your question is extraordinarily unclear.

The only way I can think of to find out which element of a random
array is zero is to traverse the array, checking the value of each
element. I don't see how knowing the previous sum of the array would

I vaguely remember some similar problem that had an interesting
solution, but I don't remember what it was.

In any case, this doesn't really look like a C question.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 25 '06 #7
sandeep <sa*******@incore.inwrote:
>Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.
That range of values happen to fit into char,
so if you are allowed to constrain arr to be that type, then

char zeebuf[1];

zeropos = snprintf(zeebuf,0,"%.*s", sizeof arr, arr );

and zeropos indicates the relative offset of the 0, with
zeropos = sizeof arr meaning that no 0 was found within the array.
[This might require UNIX98 compliance extensions]
If it is *certain* that some entry of arr has been 0'd then
instead of snprintf(), you can use zeropos = strlen() + 1
You cannot do this if arr might not have changed, because strlen()
would run off the end of arr, which would be Undefined Behaviour.
To recover the missing value, take the known sum and
subtract from it the sum of the array entries (make sure you
do the arithmetic in something a bit wider than char though!).
--
Prototypes are supertypes of their clones. -- maplesoft
Sep 25 '06 #8
On Mon, 25 Sep 2006 15:05:50 +0000, Richard Heathfield
<in*****@invalid.invalidwrote in comp.lang.c:
Michael Mair said:
jmcgill schrieb:
>
First, what is an HCL interview?
An interview by the Holy C League. About as expected as the
Spanish Inquisition.

But... but...?!?
Our two chief weapons are: undefined behavior, casting the return
value of malloc(), and void main()...

Wait...

Our three chief weapons are: undefined behavior, casting the return
value of malloc(), void main(), modifying an object twice without an
intervening sequence point...

Wait...

Out four chief weapons are...

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 26 '06 #9

In article <ef**********@canopus.cc.umanitoba.ca>, ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
sandeep <sa*******@incore.inwrote:
Have an array it contain number random Ex: arr[6] ={43,66,54,2,65,75};
There sum in 304, so suddenly one element will become zero how to find
which element has become zero and we have to find the address also.

That range of values happen to fit into char,
so if you are allowed to constrain arr to be that type, then

char zeebuf[1];

zeropos = snprintf(zeebuf,0,"%.*s", sizeof arr, arr );

and zeropos indicates the relative offset of the 0, with
zeropos = sizeof arr meaning that no 0 was found within the array.
[This might require UNIX98 compliance extensions]
C99 has the correct snprintf semantics, so if the C implementation
itself provides snprintf, then it should behave correctly. If
snprintf is coming from some other source, then you'd need to
check, as there are a lot of broken old snprintf implementations
around (Microsoft and HP among the notable suppliers).
If it is *certain* that some entry of arr has been 0'd then
instead of snprintf(), you can use zeropos = strlen() + 1
You cannot do this if arr might not have changed, because strlen()
would run off the end of arr, which would be Undefined Behaviour.
Entertaining though these approaches are, the OP suggested in a
followup that iterating over the array wasn't acceptable. Given that
constraint, I don't believe there's a solution.

--
Michael Wojcik mi************@microfocus.com

Cooperation is just like two pagodas, one hardware and one software.
-- Wen Jiabao
Sep 27 '06 #10
In article <ef*********@news2.newsguy.com>,
Michael Wojcik <mw*****@newsguy.comwrote:
>In article <ef**********@canopus.cc.umanitoba.ca>, ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>If it is *certain* that some entry of arr has been 0'd then
instead of snprintf(), you can use zeropos = strlen() + 1
>Entertaining though these approaches are, the OP suggested in a
followup that iterating over the array wasn't acceptable. Given that
constraint, I don't believe there's a solution.
We would need to hear -exactly- how the constraint was framed.
The restriction might have been against using a explicit loop/test .
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Sep 27 '06 #11
Michael Wojcik wrote:
Entertaining though these approaches are, the OP suggested in a
followup that iterating over the array wasn't acceptable. Given that
constraint, I don't believe there's a solution.
You must change a flat tire. You may not use any jack or wrench.

....If I can use a shovel and a hammer, I can do it!
Sep 28 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.