HCL interview questions can any one say solution for it

S

sandeep

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.
 
K

kondal

sandeep said:
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
 
S

sandeep

kondal said:
sandeep said:
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
 
J

jmcgill

sandeep said:
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.
 
M

Michael Mair

jmcgill said:
sandeep said:
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
 
K

Keith Thompson

sandeep said:
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
be helpful.

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.
 
W

Walter Roberson

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!).
 
J

Jack Klein

Michael Mair said:


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...
 
M

Michael Wojcik

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.
 
W

Walter Roberson

Michael Wojcik said:
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 .
 
J

jmcgill

Michael said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top