Linkedin Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

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.

The 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 :)

- Selmeczy, Péter December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

why not 1, 12 and so on

- Doctor.Sai December 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I would rephrase the question: find all words(separated from other words through tabs or spaces) in the string that can be expressed in the form of decimal number. After the words are obtained store their value in the array.

- sonu.hzb.bhr December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Deepak Garg December 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Most probably binary numbers are prefixed like 0B1100010101 or similar. At least if the hexa numbers are prefixed, and octals prefixed they should be - otherwise it is ambigous.

- Selmeczy, Péter December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use Java regular expression

- Sandra Dai January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Second Attempt March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'''find all words(separated from other words through tabs or spaces) in the string that can be expressed in the form of decimal number. After the words are obtained store their value in the array.'''

def foo(s):
	from re import findall
	return findall(r'\d+.\d+',s)

- Anonymous September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anand October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Delta November 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- yash.girdhar January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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]

- txciggy January 17, 2017 | 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