LinkedIn C++ quiz: It’s a trap!

Today LinkedIn suggested me to take C++ quiz to test my declared skill. This is a new solution of LinkedIn announced as “Skill Assessments to Help Professionals Showcase and Improve their Skills”.

I have not used C++ in “professional life” almost 4 years. Last standard I looked closely was long-awaited C++ 11. Later versions C++14 and C++17 are barely reached me. But LinkedIn comforted me that if I fail, result won’t be visible to others. Even I believed that these data will be used in matching algorithm for recruiter’s premium accounts I accepted challenge!

Question: “What is the output of the code”

#include <cstdio>
using namespace std;
 
int main()
{
  char c = 255;
  if (c > 10) {
      printf("c = %i, which is greater than 10", c);
  } else {
      printf("c = %i, which is less than 10", c);
  }
  return(0);
}

Question only looks simple of course. Obviously quiz is testing your knowledge of signed/unsigned types and min/max values, the type of an integer literal and signed/unsigned comparisons!

Also I think that quiz tries to confuse you in:

– we don’t use printf often in C++ and %i instead of traditional %d is summoned to test you. Trick is that i,d are the same for printf but match differently for fscanf/scanf according to C11.

return(0) in the original C specification parenthesis were required around the return value but nowadays it reveals old-school fan.

Select answer

  • c=-1, which is greater than 10
  • c=255, which is greater than 10
  • c=-1, which is less than 10
  • c=255, which is less than 10

Is it a Trap? You as owner of C++ skill could see the following problems

Problem 1

The question is incomplete really! It doesn’t specify platform that is critical in this question.

Max value of char std::numeric_limits<char>::max(), equivalent to CHAR_MAXSCHAR_MAX , as standards specifies:

SCHAR_MAX, maximum value for an object of type signed char 127 (27-1) or greater*.

* the actual value depends on the particular system and library implementation, but shall reflect the limits of these types in the target platform.

Yes, char is platform specific and can be greater than 8bit. But well, sure I am supposed to assume common “x86_64”, family of Linux, Windows, OSX.

Let’s assume

SCHAR_MAX == 127

but there is another greater problem

Problem 2

C++ standard says (link to early draft, C++17 standard can be purchased):

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf

If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

At the same time

6.7.1 Fundamental types [basic.fundamental]

4 Unsigned integers shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer.*

* This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

and

7.8 Integral conversions [conv.integral]

3 If the destination type is signed, the value is unchanged if it can be represented in the destination type; otherwise, the value is implementation-defined.

What about C? Standard C11

6.3.1.3 Signed and unsigned integers

3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised

What is the right answer “the behavior is undefined”, “implementation defined” or “-1 which is less than 10”?

Does the quiz expect that the user already have done this analysis in past? Or that user rely on common sense, luck or post in stack-overflow that search returned first?

After time expired, it suggested me to proceed with Next. I decided to stop quiz because it looks like a Trap really!

2 thoughts on “LinkedIn C++ quiz: It’s a trap!

  1. The person who wrote the ‘C’ language quiz needs to get a clue, too. First question was so retarded I stopped right there and didn’t care if I couldn’t take it for X more months.

    Liked by 1 person

  2. That is how any interview proceeding works, whether it’s an automated pre-screening or a final on-site. You have to be lucky to give answers an interviewer wants to hear back, and that’s it. Don’t argue to them since it might hurt their ego and your job offer.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s