Wednesday, February 7, 2007

Interesting programming problem

Here is an interesting programming problem which is quite commonly asked in job interviews nowadays. You will ask me if its so common why am I posting it here. Because, this time It comes with a twist :)

There are various constraints put on the problem to make it tricky and interesting. This is a three part problem which goes like this.

Background:
Write a function which takes a string version of a number and converts it into an integer value. For instance, "100" will become 100. There are constraints placed on the function. You must use only legal C code (no assembly) and cannot use any helper libraries (such as ANSI C functions like strlen, etc) except for the pow function. Assume all inputs are positive and valid for the sake of keeping things brief.

Question 1:
Write the aforementioned function given that the declaration is: int StringVal (const char *buf)

Question 2:
Given the same declaration, write the same function without using any looping mechanisms (for, while, goto etc).

Question 3:
Given the same declaration, write the same function without using any looping mechanisms or flow control mechanisms (for, while, if, switch, goto etc).

3 comments:

ValerĂ³n 10/28/07, 5:13 PM  

Hi, really a quite interesting problem indeed. Can I use dynamic memory control mechanism with the malloc, free...etc functions?

Vikash 10/29/07, 10:13 AM  

Yes you may use them.

ValerĂ³n 9/16/08, 1:36 PM  

Hi, vikash. I left this problem for more than 1 year, but something remind me of it these days.
My answer came up in one evening.
Would you mind to check if my answer is valid or conform the requirement?

Waiting for your reply, my e-mail is bandinfinite@hotmail.com

About This Blog

Followers

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP