Archived

This topic is now archived and is closed to further replies.

Chuk

Programming test

Recommended Posts

Chuk    122
Hello, can anyone help me with these questions I have to complete for an interview. Many thanks for your help and time. 1. Write a C or C++ function to provide the following functionality ultoa, without using library functions. The function void ultoa( unsigned long l, char* pStr, int base) should convert unsigned long l to an ascii string of base ‘base’ and place in the buffer pointed to by pStr. You may assume that pStr has enough space to hold the string and that base will be in the range 0<=base<=16. 2. Problem code This programmer wonders why “1” is printed all the time, can you tell him? #include <stdio.h> int Func1( void ) { static int n = 0; return n++; } int Func2( void ) { static int n = Func1(); return n+1; } int main() { for( ;; ) { printf( “%d\n”, Func2() ); } } This programmer heard that the max() macro was dangerous so he opted for the relative safety of an inline max() function. So why is 0 printed? #include <stdio.h> typedef unsigned short Word; inline int Max( int n, int m) { return n>m ? n : m; } Word w1=40000,w2 = 0; int main() { printf( “%d\n”, Max( w1, w2) ); return 0; } The following program (split into two files) sometimes prints “3” but prints garbage at other times under different compilers ------------------ file a1.cpp ----------------------------- #include <iostream.h> extern int f(void); extern int b; int a = f(); int main() { cout << b; return 0; } ----------------- file a2.cpp ------------------------------ extern int a; int b=a; int f() { return 3; }

Share this post


Link to post
Share on other sites
Odd the Hermit    122
quote:
Original post by Chuk
Hello,

can anyone help me with these questions I have to complete for an interview.



So, you want us to help you get a job, thus proving that you are not qualified for the position?

The sheer audacity of this is just...astounding...

-Odd the Hermit

EDIT: After looking at the questions, I'm even more astounded. They're fairly easy, really...

[edited by - Odd the Hermit on August 15, 2003 8:59:27 AM]

Share this post


Link to post
Share on other sites
ToohrVyk    1595
If that''s for an interview, then they gave you these questions to determine how good YOU are, not me (or anyone else answering). I can still try to help you by giving you some hints.

Question 1:

* You can use long operations : +,-,*,/,%,>>,<< on your long number and your base.

* Using the above operations, you can compute the nth digit of that unsigned long in the selected base (if you don''t know the math, what are you applying for anyway?).

* Once you know all digits, it''s simply a matter of filling the string with the correct digits.

* Don''t forget the ''\0''

Question 2a:

This code doesn''t work because it''s ugly. He should call these functions "Fred" or "Martha". That would be just as explicit as "Func1", but cuter.

More seriously, reading the code line after line, and determining the values taken by the variables is enough to figure out this one. Especially, look at the value of the variable n (you''d better know what static means if you''re applying for this).

Question 2b:

Look up in the first 50 pages on ANY book about learning C or C++, find the section "variables and their size", and you''ll have solved the problem.

Question 2c:

There''s no question asked here, but I assume it would be something like "why?". Try finding out in which order statements are executed and you''ll be done.

I seriously hope you don''t get additional questions the day of your interview, because if you can''t figure out these, you might have a hard time.

[Disclaimer: if you KNOW the solution to these questions, you should have posted it. Because you posted no solution of your own, I reacted like this, because I don''t feel someone who can''t get even the slightest idea about those problems shouldn''t be allowed a programmer job. If you solved these questions alone, then I''m sorry for any agressivity or irony, and I wish you best of luck.]

Share this post


Link to post
Share on other sites
Mihail121    100
Those are questions for job interview??!?!?!?Cheesh i could solve them on 14 years...

"Tonight we strike,there is thunder in the sky,together we''ll fight,some of us will die,but they''ll always remember that we''ve made a stand and many will die by hand!" - ManOwaR

Share this post


Link to post
Share on other sites
zfod    122
Heh,

These questions are easy, however some of the code is just outright wrong.

For example, question 2. That code shouldn''t even compile. So you would say in the interview, "I would tell the programmer that he''s smoking crack because that code won''t even compile".. or something similar.


lol,

.zfod

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by zfod
For example, question 2. That code shouldn''t even compile. So you would say in the interview, "I would tell the programmer that he''s smoking crack because that code won''t even compile".. or something similar.



Of course it should, what make you think it shouldn''t ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
thec    212
quote:
Original post by Mihail121
Those are questions for job interview??!?!?!?Cheesh i could solve them on 14 years...


Really? .. 14 years? . would take me a minute.

Seriously, don''t apply for the job.

Albert

Share this post


Link to post
Share on other sites
civguy    308
quote:
Original post by ToohrVyk
(((x) > (y)) ? (x) : (y))

Isn''t that a bit heavy with the parenthesis usage?
(x > y) ? x : y
would indincate the same unambiguously as well. Parenthesis around atoms or around the whole expression mean nothing.
quote:
Original post by petewood
I''d make a recommendation that in future they don''t presume all programmers are male

I thought he/him is used also when the gender is unknown. It''s just ugly to litter the text with him/her, he/she or (s)he.

Share this post


Link to post
Share on other sites
civguy    308
quote:
Original post by Chuk
This programmer heard that the max() macro was dangerous so he opted for the relative safety of an inline max() function. So why is 0 printed?

#include <stdio.h>
typedef unsigned short Word;
inline int
Max( int n, int m)
{
return n>m ? n : m;
}
Word w1=40000,w2 = 0;
int
main()
{
printf( “%d\n”, Max( w1, w2) );
return 0;
}

I wonder. I got 40000 on both GCC 3.2 and Visual C++ 7.0

Share this post


Link to post
Share on other sites
civguy    308
quote:
Original post by Fruny
Of course it should, what make you think it shouldn''t ?


“%d\n”

Illegal characters.

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by civguy
“%d\n”

Illegal characters.


And yet consistent with the way Chuk''s been using quotation marks in the rest of his post, so I doubt that''s the problem zfod is talking about.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
QUOTE: I wonder. I got 40000 on both GCC 3.2 and Visual C++ 7.0

Yeah... after looking at the problem, I''m still not certain what the problem is. The only two points of consideration are the ternary operator, and the type sizes (as someone else mentioned).

But the type sizes are easily ruled out. First of all, the type signature of the inline Max call returns int, not a short, so that coincides with the %d. Also, when variables are passed to functions in a .../vararg construct (like printf), they are promoted up... chars, shorts, and ints are promoted to longs, and floats are promoted to doubles.

I would guess it''s the ternary operator. I think that parentheses are needed around the comparison. But, I think they''re only needed for C, not C++

Share this post


Link to post
Share on other sites
civguy    308
quote:
Original post by Anonymous Poster
QUOTE: I wonder. I got 40000 on both GCC 3.2 and Visual C++ 7.0

Yeah... after looking at the problem, I''m still not certain what the problem is.
Perhaps the question is very old, from the days when int was 16-bit. Because then unsigned short 40000 would be converted to -25536, being smaller than 0.

Share this post


Link to post
Share on other sites
cgoat    132
quote:
Original post by Anonymous Poster
QUOTE: I wonder. I got 40000 on both GCC 3.2 and Visual C++ 7.0

Yeah... after looking at the problem, I''m still not certain what the problem is.


I''d wager the problem is either that they typod the test, or the test was misquoted, and its supposed to be signed not unsigned short.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
#include <stdio.h>
typedef unsigned short Word; **** Looky Looky
inline int
Max( int n, int m) **** Type converison unsigned to signed !!!!
{
return n>m ? n : m; **** So n will become negative and m will be greater
}
Word w1=40000,w2 = 0; **** w1, w2 are unsigned short integers
int
main()
{
printf( “%d\n”, Max( w1, w2) );
return 0;
}

Share this post


Link to post
Share on other sites
Odd the Hermit    122
Ok, I understand the attraction of arguing over some of these questions, but aren''t we, in doing so, helping Chuk in a test for a position he''s not qualified for? Shouldn''t we (all) have just looked at his post, shaken our heads in dismay, and went on with our business elsewhere?

-Odd the Hermit

Share this post


Link to post
Share on other sites
civguy    308
Odd the Hermit: SILENCE! (it was supposed to sound menacing but now it just sounds angry)
quote:
Original post by Anonymous Poster
#include <stdio.h>
typedef unsigned short Word; **** Looky Looky
inline int
Max( int n, int m) **** Type converison unsigned to signed !!!!
{
return n>m ? n : m; **** So n will become negative and m will be greater
}
Word w1=40000,w2 = 0; **** w1, w2 are unsigned short integers

Wow, you're so smart. But n will not become negative because a signed integer can easily hold any unsigned short on 32-bit computers.

[edited by - civguy on August 15, 2003 2:57:12 PM]

Share this post


Link to post
Share on other sites
DarkVoodoo    122
quote:
Original post by Anonymous Poster
#include <stdio.h>
typedef unsigned short Word; **** Looky Looky
inline int
Max( int n, int m) **** Type converison unsigned to signed !!!!
{
return n>m ? n : m; **** So n will become negative and m will be greater
}
Word w1=40000,w2 = 0; **** w1, w2 are unsigned short integers
int
main()
{
printf( “%d\n”, Max( w1, w2) );
return 0;
}




As civguy pointed out, this is true if int is the same size as short, however it is not the case on today''s 32 bit systems(that I know of, feel free to correct me). IMHO, to make sure there is a problem, max should have been written like :

inline int Max(short int n, short int m)

( I think its the first time I write code to create a problem instead of fixing one)

Share this post


Link to post
Share on other sites
civguy    308
quote:
Original post by Ernest
int main() should return something...
int main() is special. If it doesn''t have a return statement at the bottom, then it''s defined to return 0. So "int main() {}" is legal code. At least in C++, I''m not sure about C.

Share this post


Link to post
Share on other sites
Odd the Hermit    122
quote:
Original post by civguy
int main() is special. If it doesn''t have a return statement at the bottom, then it''s defined to return 0. So "int main() {}" is legal code. At least in C++, I''m not sure about C.



Actually, MSVC 6 complains if you don''t have a return value. You can declare main() to be void, which gets rid of the issue...

-Odd the Hermit

Share this post


Link to post
Share on other sites
civguy    308
quote:
Original post by Odd the Hermit
Actually, MSVC 6 complains if you don''t have a return value. You can declare main() to be void, which gets rid of the issue...
MSVC 6 is so buggy that I don''t consider it as a C++ compiler.

Share this post


Link to post
Share on other sites