Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey17673" class="run-this">first convert heximal value into string (or char[])

int HexToDec(string hex)
{
int dec(0);

size_t len = hex.size();
int _16mul = 1;
for(size_t i = len; i > 0; --i) {
char number = static_cast<char>(tolower(hex[i - 1]));
if(number >= '0' && number <= '9')
dec += (number - '0') * _16mul;
else if(number >= 'a' && number <= 'f')
dec += (number - 'a' + 10) * _16mul;
else cout << "wrong symbol: " << number << endl;
_16mul *= 16;
}

return dec;
}</pre><pre title="CodeMonkey17673" input="yes">
</pre>

- Anonymous December 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I`d just make a code a little smaller and more "clean"


for(size_t i = len, _16mul = 1; i > 0; --i, _16mul*=16)

- Anonymous December 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

C#
int HexToDec(string hex)
{
int i = Int32.Parse(hex, NumberStyles.HexNumber);
return i.ToString();
}

- Yuan Yao December 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

блядь, ну откуда вы такие умные беретесь, а то без вас никто бы не догадался использовать готовые библиотечные функции

- Anonymous December 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int HexDecimalMap(char ch)
{
   if (ch >= '0' && ch <='9')
   {
       return (ch-'0');
   }
   if (ch >= 'a' && ch <= 'f')
   {
       return (10+ (ch-'a');
   }
   else if (ch >= 'A' && ch <= 'F')
   {
       return (10+ (ch-'A');
   }
   else
   {
      return -1;   
   }
}

int HexToDecimal(char *str)
{
   int num = 0;
   while (*str)
   {
      int numMap = HexDecimalMap(str++);
      if (numMap < 0)
      {
         return -1; //error
      }
      num = num * 16 + numMap;
   }
   return num;
}

- r.ramkumar December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@r.ramkumar: This has a small bug(coversion bug):The while loop should be like

while(*str)
{
char ch=*str;
str++;
int numMap=HexDecimalMap(ch);
......rest code is fine

- Anonymous December 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well I would say there is a bug but not the one you pointed out. The bug is num is not incremented. num should be incremented at the end of while loop.

- sumit saxena January 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ramkumar : I think there is a bug. You are multiplying the MSB(left->right in the input) 0*16+numMap,

This needs to be done actually for the LSB.

- KingKong Jnr February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, I see ramkumar's code as correct. No need to increment num and parsing input from left to right is more efficient.

- Anonymous November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = Int32.Parse(hex, NumberStyles.HexNumber);

}

- weijiang2009 February 06, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More