## Linkedin Interview Question

Software Engineer / Developers**Country:**India

**Interview Type:**In-Person

Couple of question which needs to clarify ?

1. will string contain decimal number ? if yes then how would you identify between decimal and binary number.

for example : string "abcd 0xa 1111 123 1010" In this we are not able to find the number ( 1010 ,1111 ) in binary or decimal

There is nothing to explain the algorithm really, just a bunch of conditions.

```
bool validateNum(string in)
{
int i=-1;
bool hexFound = false;
bool decFound = false;
bool numFound = false;
bool zeroFound = false;
bool signFound = false;
if(in[0]=='+' || in[0]=='-') { i=1; signFound=true;}
else i=0;
for(; i<in.length(); i++)
{
if(in[i]=='.')
{
if(hexFound || decFound || !numFound) return false;
decFound = true;
}
else if(in[i]=='x')
{
if(signFound || hexFound || decFound || !numFound || !zeroFound) return false;
hexFound = true;
}
else if(in[i]=='0')
{
numFound = true; zeroFound=true;
}
else if((in[i]>='0' && in[i]<='9') || (in[i]>='A' && in[i]<='F') || (in[i]>='a' && in[i]<='f')) { numFound = true; }
else return false;
}
return true;
}
```

```
public static boolean isNumber(String str) {
int n = str.length();
boolean hexFound = false;
boolean decimalFound = false;
boolean numFound = false;
boolean signFound = false;
boolean zeroFound = false;
for(int i = 0; i < n ; i++) {
char ch = str.charAt(i);
switch(ch) {
case '-':
case '+':
if(signFound || i != 0) {
return false;
} else {
signFound = true;
}
break;
case '.':
if(hexFound || decimalFound || i == n-1) {
return false;
} else {
decimalFound = true;
}
break;
case 'x':
if(!zeroFound || hexFound || numFound || signFound) {
return false;
} else {
hexFound = true;
}
break;
case '0':
zeroFound = true;
break;
default:
if(ch >= 'a' && ch <='f') {
if(!hexFound) {
return false;
}
} else if(ch >= '0' && ch <= '9') {
numFound = true;
} else {
return false;
}
}
}
return true;
}
```

```
public static void parser(String ip){
if(ip == null || ip.trim()== "")
return;
String arr[] = ip.split("\\s");
//create a arraylist of patterns to be match
ArrayList<Pattern> patt = new ArrayList<Pattern>();
patt.add(Pattern.compile("[0-9]*.[0-9]*"));
patt.add(Pattern.compile("[0-9]*"));
patt.add(Pattern.compile("0x[a-fA-F0-9]*"));
for(int i =0; i< arr.length; i++){
for(int j =0; j < patt.size(); j++){
Matcher m = patt.get(j).matcher(arr[i]);
if(m.matches()){
System.out.println(arr[i]);
break;
}
}
}
}
```

for a particular word:

It works for all cases:

```
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);
}
```

Python regex:

```
import re
def num_convert(s):
ret = []
for w in s.split():
if re.match("^0x[0-9a-fA-F]+$", w):
ret.append(int(w,16))
elif re.match("^0b[01]+$", w):
ret.append(int(w,2))
elif re.match("^[0-9]+$", w):
ret.append(int(w))
elif re.match("^[0-9\.]+$", w):
ret.append(float(w))
return ret
```

=====================

>>> num_convert("abcd 0xa 11.12 123 0b110 12.0")

[10, 11.12, 123, 6, 12.0]

I think this question is about to check if you know what a lexical unit parser is, how to define grammar rules and how to implement it. BNF notation is a must. To code it with a state machine is quite simple with one character look-ahead.

- Selmeczy, Péter December 15, 2011The other way around can be using regular expressions, and some RegEx class, but this is again about testing if you know how to define the expression of what a number is.

number := decimal_number | hexa_number | octal_number | binary_number

decimal_number: digits | digits . digits

digits := digit digits | digit

digit := 0|1|2|3|4|5|6|7|8|9

hexa_number := (0x | 0X) hexa_digits

hexa_digits := hexa_digit hexa_digits | hexadigit

hexa_digit := 0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|a|b|c|d|e|f

and so on. Yes, well spot that decimal numbers cannot start with 0, that's what is for octals, so carry on and correct the rule :)