## Linkedin Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

``````bool isNumber(char *str)
{

trim(str); // remove leading and trailing spaces.

int i=0;
bool dec=false; //is decimal?
while(str[i])
{
if(str[i] == '-' || str[i] == '+'){
if(i!=0) return false;
}
else if(str[i] == '.'){
if(dec)
return false; //we can have only one decimal point in a number
else
dec = true;

if(!str[i+1])  // make sure decimal is not last char in the string
return false;
}
else if(str[i] <= '0' || str[i]>='9') return false;
i++;
}
}``````

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

Looks nice. Do we need to consider binary or hex representation?

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

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

last condition should be (str[i] < '0' || str[i] > '9')

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

Need to think about all the following cases
1. Positive/Negative Integer,
2. Positive/Negative decimal with only one dot(.)
3. Hex/Oct/Binary number, like 0xff(hex), 023(Oct),
4. E: such as 1.3E2333343
5. Unicode: \u0034

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

<pre lang="" line="1" title="CodeMonkey52161" class="run-this">/* Assuming the string is ascii and would denote only +ve numbers.
If negative numbers are allowed, we'd need an additional step to check if first character of the string is a '-' */
class NumCheck{
public static boolean isNumber(String str)
{
boolean isNum=true;
for (int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(!(c>47 && c<58)) //checking if it lies in the ascii range for numbers
{
isNum= false;
break;
}
}
return isNum;
}
public static void main(String[] args)
{
System.out.println(isNumber("256")?"yes":"no");
System.out.println(isNumber("2fifty6")?"yes":"no");
}
}
</pre><pre title="CodeMonkey52161" input="yes">
</pre>

Comment hidden because of low score. Click to expand.
1
of 3 vote

return inputString,matches("[+-]?//d+(.//d+)?");

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

I believe you meant

``````public static boolean isNumeric(String input){

return input.matches("[-+]?\\d+(\\.\\d+)?");
}``````

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

What type of number? if a string is Integer or if a string is a Double

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

ASCII comes to rescue here !!

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

Iterate over the characters and check if the ascii is between 48 and 57. Its a number.

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

Negative numbers were not considered here

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

This is very tedious. On an interview, the interviewer is looking to test your carefulness, etc.

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

A pretty good solution over here h t t p ://rosettacode.org/wiki/Determine_if_a_string_is_numeric

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

i=0;
while(ar[i] >= '0' && ar[i] <= '9')
i++;
check if i is last character or not.

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

``````boolean isNumber = false;
try {
Double.parseDouble(inputStr)
isNumber = true;
} catch (NumberFormatException e) {

}``````

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

if inputStr is 007, parseDouble still returns true...
The problem is that how to clearly define "number"

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

``````bool validateNum(string in)
{
bool decimalFound = false, numFound = false;
int i=-1;

if(in[0]=='+' || in[0]=='-') i = 1;
else i=0;

for(; i<in.length(); i++)
{
switch(in[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': numFound = true;
break;
case '.':
if(decimalFound) return false;
else if(numFound) decimalFound = true;
else return false;
break;
default:
return false;
}
}
return true;
}``````

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

Using explicit state variable makes it easier to extend the syntax covered.
This program covers octal, decimal, and hex integer numbers and both decimal and hex floating point number (without exponent).

``````#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

enum State {
s_start,
s_sign,
s_0,
s_0x,
s_octal,
s_decimal,
s_hex,
s_decdot,
s_hexdot,
s_nan,
};

bool isNumber(string & str)
{
State state = s_start;

for (int i = 0; i < str.length(); i++) {
char c = str[i];
State next_state = s_nan;

switch(state) {
case s_start:
if (('+' == c) || ('-' == c)) {
next_state = s_sign;
} else if ('0' == c) {
next_state = s_0;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;

case s_sign:
if ('0' == c) {
next_state = s_octal;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;

case s_0:
if (('x' == c) || ('X' == c)) {
next_state = s_0x;
} else if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
} else if ('.' == c) {
next_state = s_decdot;
}
break;

case s_octal:
if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
}
break;

case s_decimal:
if ('.' == c) {
next_state = s_decdot;
break;
}
// Fallthrough
case s_decdot:
if (('0' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;

case s_hex:
if ('.' == c) {
next_state = s_hexdot;
break;
}
// Fallthrough
case s_hexdot:
case s_0x:
if ((('0' <= c) && (c <= '9'))
|| (('a' <= c) && (c <= 'f'))
|| (('A' <= c) && (c <= 'F'))) {
next_state = s_hex;
}
break;

case s_nan:
return false;
}

state = next_state;
}

if ((s_octal == state) || (s_decimal == state) || (s_hex == state) || (s_0 == state)) {
return true;
}

return false;
}

#include <cstdlib>

int main(int argc, char *argv[])
{
const char *s = "";

if (argc > 1) {
s = argv[1];
}

string str(s);
bool isanum = isNumber(str);
cout << "'" << str << "'" << " is" << (isanum ? "" : " not") << " a number" << endl;

return (0);
}``````

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

Minor correction. Adding decimal_point_seen simplifies the program a little. s_decimaldot, s_0dot, and s_hexdot are added to enforce having at least one digit after the decimal point of decimal and hex floating point numbers.

``````#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

enum State {
s_start,
s_sign,
s_0,
s_0x,
s_0dot,
s_octal,
s_decimal,
s_decimaldot,
s_hex,
s_hexdot,
s_nan,
};

bool isNumber(string & str)
{
bool decimal_point_seen = false;
State state = s_start;

for (int i = 0; i < str.length(); i++) {
char c = str[i];
State next_state = s_nan;

switch(state) {
case s_start:
if (('+' == c) || ('-' == c)) {
next_state = s_sign;
} else if ('0' == c) {
next_state = s_0;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;

case s_sign:
if ('0' == c) {
next_state = s_octal;
} else if (('1' <= c) && (c <= '9')) {
next_state = s_decimal;
}
break;

case s_0:
if (('x' == c) || ('X' == c)) {
next_state = s_0x;
} else if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
} else if ('.' == c) {
decimal_point_seen = true;
next_state = s_0dot;
}
break;

case s_octal:
if (('0' <= c) && (c <= '7')) {
next_state = s_octal;
}
break;

case s_decimal:
case s_0dot:
case s_decimaldot:
if (('0' <= c) && (c <= '9')) {
next_state = s_decimal;
} else if ('.' == c) {
if (! decimal_point_seen) {
decimal_point_seen = true;
next_state = s_decimaldot;
}
}
break;

case s_hex:
case s_hexdot:
if ('.' == c) {
if (! decimal_point_seen) {
decimal_point_seen = true;
next_state = s_hexdot;
}
break;
}
// Fallthrough
case s_0x:
if ((('0' <= c) && (c <= '9'))
|| (('a' <= c) && (c <= 'f'))
|| (('A' <= c) && (c <= 'F'))) {
next_state = s_hex;
}
break;

case s_nan:
return false;
}

state = next_state;
}

if ((s_octal == state) || (s_decimal == state) || (s_hex == state) || (s_0 == state)) {
return true;
}

return false;
}

#include <cstdlib>

int main(int argc, char *argv[])
{
const char *s = "";

if (argc > 1) {
s = argv[1];
}

string str(s);
bool isanum = isNumber(str);
cout << "'" << str << "'" << " is" << (isanum ? "" : " not") << " a number" << endl;

return (0);
}``````

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

``````public static boolean isNumeric(String inputData) {

return inputData.matches("[-+]?[0-9]+([.][0-9])?");
}``````

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

``````bool isNumber(char *s) {
if (s == NULL || *s == '\0')
return 0;
char * p;
strtod (s, &p);
// cout<<p<<endl;
if(!strcmp(s," "))
return false;
if(*p==' '){
// cout<<"if\n";
while(*p==' ')
p++;
}
// cout<<strlen(p)<<endl;
return (*p == '\0' || !strlen(p)>0);
}``````

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.

### 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.

### 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.

### 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.