Linkedin Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Please go through the string each char by char
compare with ASCII value if it is between 48 to 57.
Then it is a num.

- Hari April 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

makes sense.. also first time u find a char break the loop and return false

- An Enthusiast April 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A number could be decimal also. So also need to check that there is atmost 1 dot character and it is not present in the beginning or end.

- kr.neerav April 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

/*
     * Returns true if the input string is a number and false otherwise
     */
    /*
     * Examples:
     * isNumber(12) -> true
     * isNumber("one") -> false
     * isNumber(23.2) -> true
     * isNumber(0) -> true
     * isNumber(-23) -> true
     */

public static boolean isNumber(String toTest)
    {
        boolean flag = false;
        
        // implementation here
        String pattern = "(-?\\d+)|(-?\\d+\\.\\d+)";
        flag = toTest.matches(pattern);
        return flag;

}

- madhur.eng April 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like this solution. However if we are going to use Java's fancy functions why not just do this:

public static boolean isNumber(String toTest)
    {
        try
        {
             Double.parseDouble(toTest); //Throws exception if not a valid number
             return true;
        }
        catch(Exception e)
        {
             return false;
        }
    }

- Anonymous May 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a better regex:

"-?\\d+(\\.\\d+)?"

- remosewa August 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

public static long parseInt (String s) throws NumberFormatException {
		
		boolean neg = false;

		if(s.startsWith("-")){
			neg = true;
			s = s.substring(1);
		}
		int len = s.length();

		long result = 0;
		for(int i=0;i<len;i++) {
			if(s.charAt(i) < 48 || s.charAt(i) > 57)
				throw new NumberFormatException("Invalid Number");
			int test = (s.charAt(i) - '0');
			result = result * 10;
			result = result + test;
		}
		if(neg){
			return -result;
		}
		return result;
	}

--Use the above function and catch NumberFormatException to determine isNumber.

- Balaji April 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am down voting the same because the use of regex expression is the best way to do so as that is much more quicker and clean.

- madhur.eng April 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I personally like the regex solution but wonder if that's what the interviewers are looking for...so i just implement a function to check if a string is a number.

public static boolean isDigit(char c){
    if(c>='0' && c<='9'){
        return true;
    }else{
        return false;
    }
}

public static boolean isNumber(String s){

    if(s.length() == 0){
        return false;
    }
    
    boolean dotUsed = false;
    
    for(int i=0;i<s.length();i++){
        if(i==0){
        //first digit can only be -, ., or 0-9
            if(s.charAt(i) == '-'){
                continue;
            }
            
            if(s.charAt(i) == '.'){
                dotUsed = true;
                continue;
            }
            
            if(!isDigit(s.charAt(i))){
                return false;
            }
        }else{

            //only one dot allowed
            if(s.charAt(i) == '.'){
            
                if(dotUsed){
                    return false;  
                }else{
                
                    //dont allow "10."
                    if(i==s.length()-1){
                        return false;
                    }else{
                        dotUsed = true;
                        continue;
                    }
                }
            } 
            
            //dont allow cases like 0000002 
            if(i==1 && s.charAt(0) == '0' && s.charAt(1)=='0'){
                return false;
            }  
            
            if(!isDigit(s.charAt(i))){
                return false;
            }      
        }
            
    }
    
    return true;

}

- sh88990 April 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The rules to be implemented depends on what is defined as a number. The following program accepts one + or - as prefix, one .(dot) at anywhere except the end, arbitrary number of 0s as prefix.

public class IsNumber {
	//valid numbers: -1, -1.2, +1, +0.1, +1.0
	//invalid numbers: --1, -+1. -1.2.3
	public static boolean isN(String str) {
		boolean hasSign = false;
		boolean hasDot = false;
		
		if (str==null || str.length()<1) return false;

		char c = str.charAt(0);

		if (c=='-'||c=='+') hasSign = true;
		else if (c=='.') hasDot = true;
		else if (c>'9' || c<'0') return false;

		for (int i=1; i<str.length(); i++) {
			c = str.charAt(i);
			if (hasSign && (c=='+' || c=='-')) return false;
			else if (c=='.') {
				if (hasDot) return false;
				else hasDot = true;
			}
			else if (c>'9' || c<'0') return false;
		}
		return str.charAt(str.length()-1)!='.'; //exclude last char be dot.
	}
	
	public static void main(String[] args) {
		String[] testStr = new String[] {"-1", "-1.2", "+1", "+1.0", ".3", "1.3", "c.3", "--1", "-+1", "1.2.3", "-1.2.3", "3.", "12c" };
		for(String s:testStr) {
			System.out.printf("%b, ", isN(s));
		}
	}
}

- sabz July 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TestNumeric {

	private boolean isNumeric(String input) {
		// TODO Auto-generated method stub
		try {
			double number = Double.parseDouble(input);
		} catch (NumberFormatException exception) {
			return false;
		}
		return true;
	}

	public boolean isNumber(String toTest) {
		
		toTest = toTest.trim();
		// implement this
		if (toTest == null)
			return false;
		else {
			int i = 0;
			if (toTest.charAt(i) == '-') {
				i++;
			}
			int floatPointCounter = 0;
			while (i < toTest.length()) {
				if (toTest.charAt(i) == '.' && floatPointCounter < 1) {
					floatPointCounter++;
				} else if (toTest.charAt(i) > '9' || toTest.charAt(i) < '0'
						|| floatPointCounter > 1) {
					return false;
				}
				i++;
			}
			return true;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String input = "   -5";
		TestNumeric tN = new TestNumeric();
		System.out.println("Numeric Status: " + tN.isNumeric(input));
		System.out.println("Numeric Status: " + tN.isNumber(input));

	}

}

- sLion August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

I'm assuming you can just use Integer.parseInt?

public boolean isNumber(String toTest)
{
	try
	{
		Integer.parseInt(toTest);
		return True;
	}
	catch (NumberFormatException e)
	{
		return False;
	}
}

A little bit of Python logic (using exceptions to test), but should work just fine.

- Grendus April 03, 2014 | 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