Domanda di colloquio di Cornerstone OnDemand

The hardest problem posed was the C# function I had to write. Write a function that accepts a character parameter such as "8" and then returns its digit value which would be 8 in this case. You cannot use any helper functions, casting or conversion functions. int ParseCharToInt(char c)

Risposte di colloquio

Anonimo

22 giu 2016

int GetDigitFromInput(char input){if (('9' - input) < 0)return -1;// or throw new Exception("Numeric Char Only"); return input - '0';}

2

Anonimo

6 dic 2014

well.. this can be a little bit of a cheat but one character can only be from 0 to 9, +ve numbers. A simple dictionary like charMap should do the trick. This will work in O(1) time complexity and O(n) or O(c) space complexity.

1

Anonimo

7 ott 2015

You could also use a switch statement: int ParseChar(char number) { switch(number) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; etc.. } throw new Exception('Invalid input"); } If you could assume only valid inputs, then you could just subtract '0' from the value: int ParseChar(char number) { return number - '0'; }

1

Anonimo

25 gen 2013

Even Google didn't have the answer to this problem.