Microsoft Interview Question for Software Developers


Team: Cloud + Enterprise
Country: United States
Interview Type: Written Test




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

public static bool ConvertToNumber(string str)

First, the name should be something like IsConvertibleToNumber, since the number isn't returned. Either that or the number should be returned, whichever is more useful. But if we want to return the number plus a boolean saying whether the conversion was successful, there are already standard library methods for this, such as Int16.TryParse.

Furthermore, we might be inclined to question whether this method is supposed to handle things that are not Int16. If yes, we're missing a lot of logic. If not, the method should be named IsConvertibleToInt16.

bool canConvert = false;
try
{
    int n = Int16.Parse(str);
 
    if (n != 0)
    {
        canConvert = true;
    }
}
catch (Exception ex)
{
                
}

Try-catch should not be used for ordinary control flow. Testing for n != 0 doesn't make sense because the parsed number could be a zero.

bool retval = false;
if (canConvert == true)
{
    retval = true;
}
return retval;

Everything here can just be replaced with

return canConvert;

The best way to write this method would have been as a thin wrapper around TryParse. In that case, the method saves you from having to declare a temporary variable.

public static bool IsConvertibleToInt16(string str)
{
    Int16 val;
    return Int16.TryParse(str, out val);
 }

- eugene.yarovoi March 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about redundant conversion code, isn't it a waste of resources to parse string just in order to figure out whether it's parsable?

- c4melot March 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Int16.Parse(str) may throw some exception for invalid str.
Hence the code has try catch block

- sv March 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The tryParse solution is great. If we still using Int16.Parse, we can similarly modify the code. If the number cannot be parsed an exception will be thrown and caught by the catch block. Otherwise we have successfully parsed the number and we can return true.The code should be:

public static bool ConvertToNumber(string str)
        {
            try
            {
                int n = Int16.Parse(str);
            }
            catch (Exception ex)
            {
                cout<<"Invalid number\n"<<endl;
            }        
            return true;

- sowmya.kp May 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. ConvertToNumber – Find Flaws and Limitations

Identify the flaws / limitations in the following ConvertToNumber method
public static bool ConvertToNumber(string str)
{
bool canConvert = false;
try
{
int n = Int16.Parse(str);

if (n != 0)
{
canConvert = true;
}
}
catch (Exception ex)
{

}
bool retval = false;
if (canConvert == true)
{
retval = true;
}
return retval;
}

- Anonymous June 25, 2019 | 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