Microsoft Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

c# implementation.
hash tables.

using System;
using System.Collections.Generic;
using System.Text;

namespace TextToNumber {

    class Program {

        static private string Convert( string s ) {

            var res = new StringBuilder();

            var arr = s.ToLower().Split(' ');

            int startPos = -1; int hundredPos = -1; int tmpCalc = 0;

            bool firstTimeHere = true;

            for ( int i = 0; i < arr.Length; i++ ) {

                int t;
                if ( arr[ i ] == "hundred" ){
                    hundredPos = i; t = i - 1;
                    while ( t > startPos ) {
                        tmpCalc += dic1[ arr[ t ] ];
                        t--;
                    }
                    tmpCalc *= 100;
                }

                var containsVal = dic2.ContainsKey( arr[ i ] );
                if ( !containsVal && i != arr.Length - 1 ) {
                    continue;
                }

                t = containsVal ? i - 1 : i;
                while ( t > ( hundredPos != -1 ? hundredPos : startPos ) ) {
                    tmpCalc += dic1[ arr[ t ] ];
                    t--;
                }
                startPos = i;
                hundredPos = -1;

                if ( firstTimeHere ) {
                    res.Append( tmpCalc.ToString() );
                    if ( containsVal ) {
                        for ( int j = 0; j < dic2[ arr[ i ] ]; j++ ) {
                            res.Append( "0" );
                        }
                    }
                    firstTimeHere = false;
                } else {
                    int pos = res.Length - ( containsVal ? dic2[ arr[ i ] ] : 0 ) - 3;
                    res.Remove( pos, 3 );
                    res.Insert( pos, tmpCalc.ToString().PadLeft( 3, '0' ) );
                }
                tmpCalc = 0;
            }
            return res.ToString();
        }

        static readonly Dictionary<string, byte> dic1 = new Dictionary<string, byte> {
            { "one", 1 }, { "two", 2 }, { "three", 3 }, { "four", 4 }, { "five", 5 },
            { "six", 6 }, { "seven", 7 }, { "eight", 8 }, { "nine", 9 },{ "ten", 10 },
            { "eleven", 11 }, { "twelve", 12 }, { "thirteen", 13 }, { "fourteen", 14 },
            { "fifteen", 15 }, { "sixteen", 16 }, { "seventeen", 17 },{ "eighteen", 18 },
            { "nineteen", 19 }, { "twenty", 20 }, { "thirty", 30 }, { "forty", 40 },
            { "fifty", 50 }, { "sixty", 60 },{ "seventy", 70 },{ "eighty", 80 },{ "ninety", 90 },
        };

        static readonly Dictionary<string, byte> dic2 = new Dictionary<string, byte> {
            { "thousand", 3 },{ "million", 6 }, { "billion", 9 },
            { "trillion", 12 } // any more if needed
        };

        static void Main( string[] args ) {
            var str = "One million two hundred thousand fifty seven";
            var res = Convert( str );
            Console.WriteLine( res );
        }
    }
}

- zr.roman January 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this was the correct approach.

- tamashionuth January 03, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Another way to do this:
Write a Function to ConvertStringToInt() for a Number < 1000.
Then reuse this function over every boundary of billion, million, thousand,..

- anilnuru January 04, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

small note:
the rules for writing numbers do not allow "s", i.e. "One million two hundred thousand fifty seven", not "hundreds" and not "thousands" (not "millions", etc.)

- zr.roman January 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 *
 * @author ugurdonmez
 */
public class IntegerConvert {
    
    public static void main(String [] args) {
        
        IntegerConvert iConvert = new IntegerConvert();
        
        System.out.println(iConvert.convert("one million two hundread"));
        System.out.println(iConvert.convert("one million two hundread thousand"));
        System.out.println(iConvert.convert("one million two hundread thousand fifty seven"));
        System.out.println(iConvert.convert("one million two hundread thousand two hundread fifty seven"));
        
    }
    
    private final HashMap<String, Integer> map;
    
    public IntegerConvert() {
        
        map = new HashMap<>();
        
        map.put("zero", 0);
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);
        map.put("four", 4);
        map.put("five", 5);
        map.put("six", 6);
        map.put("seven", 7);
        map.put("eight", 8);
        map.put("nine", 9);
        
        map.put("ten", 10);
        map.put("eleven", 11);
        map.put("twelve", 12);
        map.put("thirteen", 13);
        map.put("fourteen", 14);
        map.put("fifteen", 15);
        map.put("sixteen", 16);
        map.put("seventeen", 17);
        map.put("eightteen", 18);
        map.put("nineteen", 19);
        
        map.put("twenty", 20);
        map.put("thirty", 30);
        map.put("fourty", 40);
        map.put("fifty", 50);
        map.put("sixty", 60);
        map.put("seventy", 70);
        map.put("enighty", 80);
        map.put("ninety", 90);
        
        map.put("hundread", 100);
        map.put("thousand", 1000);
        map.put("million", 1000000);
        map.put("billion", 1000000000);
        
    }
    
    public int convert(String string) {
        
        int value = 0;
        
        String[] words = string.split(" ");
        
        int local = 0;
        
        for ( String s : words ) {
            
            if ( isMultiply(s) && s.equals("hundread") ) {
                local *= map.get(s);
            } else if (isMultiply(s)){
                local *= map.get(s);
                value += local;
                local = 0;
            } else {
                local += map.get(s);
            }
        }
        
        value += local;
        return value;
    }
    
    public boolean isMultiply(String s) {
        
        if ( s.equals("billion") || s.equals("million") || s.equals("thousand") || s.equals("hundread") ) {
            return true;
        } else {
            return false;
        }
    }
}

- ugurdonmez87 January 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

does not work on "one hundred thousand"

- zr.roman January 07, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;

public class StringToNumber {

public static void main(String[] args)
{
String alphaNum = "One Million Thirty Two Thousand Thirty Two";
int result = getNum(alphaNum);
System.out.println("Result is " + result);
}

private static int getNum(String alphaNum) {
// TODO Auto-generated method stub
HashMap<String, Integer> converter = new HashMap<>();

//adding the String to numbers conversions into hashmap

converter.put("One", 1);
converter.put("Two", 2);
converter.put("Three", 3);
converter.put("Four", 4);
converter.put("Five", 5);
converter.put("Six", 6);
converter.put("Seven", 7);
converter.put("Eight", 8);
converter.put("Nine", 9);
converter.put("Ten", 10);
converter.put("Eleven", 11);
converter.put("Twelve", 12);
converter.put("Thirteen", 13);
converter.put("Fourteen", 14);
converter.put("Fifteen", 15);
converter.put("Sixteen", 16);
converter.put("Seventeen", 17);
converter.put("Eighteen", 18);
converter.put("Ninteen", 19);
converter.put("Twenty", 20);
converter.put("Thirty", 30);
converter.put("Fourty", 40);
converter.put("Fifty", 50);
converter.put("Sixty", 60);
converter.put("Seventy", 70);
converter.put("Eighty", 80);
converter.put("Ninety", 90);

converter.put("Billion", 1000000000);
converter.put("Million", 1000000);
converter.put("Thousand", 1000);
converter.put("Hundred", 100);

//split the input string and read it from left to right and keep adding
String[] words = alphaNum.split(" ");
int i=0;
int result = 0, Num1=0, Num2=0, Num3=0;
String word1="", word2="", word3 ="";
while(i<words.length)
{
Num1 = 0;
Num2 = 0;
Num3 = 0;
word1 = "";
word2 = "";
word3 = "";

word1 = words[i++];
Num1 = converter.get(word1);

if(i<words.length)
{
word2 = words[i++];
Num2 = converter.get(word2);

}

//System.out.println(result);
if(Num2==100 || Num2==1000 || Num2==1000000 || Num2==1000000000)
{
System.out.println(result);

result += Num1 * Num2;
System.out.println("if" + result);

}
else
{
if(i<words.length)
{
word3 = words[i++];
System.out.println("word3 " + word3);
Num3 = converter.get(word3);
result += (Num1 + Num2) * Num3;
}
else
{
result += Num1 + Num2;
}

System.out.println("else" + result);

}

}


return result;
}
}

- Zesta January 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;
import java.util.Map;

// string to number 
// e.g. One million two hundred thousands fifty seven
// should be converted to 1200057

class Main{
    private static Map<String,Integer> dic1 = new HashMap<String,Integer>();
    private static Map<String,Integer> dic2 = new HashMap<String,Integer>();
    static{
    	dic1.put("one",1);
    	dic1.put("two",2);
    	dic1.put("three",3);
    	dic1.put("four",4);
    	dic1.put("five",5);
    	dic1.put("six",6);
    	dic1.put("seven",7);
    	dic1.put("eight",8);
    	dic1.put("nine",9);
    	dic1.put("ten",10);
    	dic1.put("eleven",11);
    	dic1.put("twelve",12);
    	dic1.put("thirteen",13);
    	dic1.put("fourteen",14);
    	dic1.put("fifteen",15);
    	dic1.put("sixteen",16);
    	dic1.put("seventeen",17);
    	dic1.put("eighteen",18);
    	dic1.put("nineteen",19);
    	dic1.put("twenty",20);
    	dic1.put("thirty",30);
    	dic1.put("fourty",40);
    	dic1.put("fifty",50);
    	dic1.put("sixty",60);
    	dic1.put("seventy",70);
    	dic1.put("eighty",80);
    	dic1.put("ninty",90);
    	
    	dic2.put("hundred",100);
    	dic2.put("thousand",1000);
    	dic2.put("million",1000000);
    	dic2.put("billion",1000000000);
    }
    static int convert(String numString){
        String[] words = numString.split(" +");
        int num=0;
        for(int i =words.length-1;i>=0;i--){
            String word = words[i];
            if(dic1.containsKey(word)){
                num+=dic1.get(word);
            }else{
                int val = dic2.get(word);
                String qualifyingWord;
                while(i>=0)
                {
                    i--;
                    qualifyingWord = words[i];
                    if(dic1.containsKey(qualifyingWord)){
                        val = val*dic1.get(qualifyingWord);
                        num+=val;
                        break;
                    }else{
                        val = val*dic2.get(qualifyingWord);
                    }
                }
            }
        }
        return num;
    }

    public static void main(String args[]){
    	System.out.println(convert("one hundred thousand"));
    }
}

- Anonymous February 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static String[] numNames={"","one","two","three","four","five","six","seven","eight","nine","ten","eleven",
			"twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"};
	static String[] tensNames={"","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
	static String[] specialNames={"","thousand","million","billion","trillion","quadrillion","quintillion"};
public static String convertLessThanOneThousand(int number) {
        String current;
        
        if (number % 100 < 20){
            current = numNames[number % 100]+" ";
            number /= 100;
        }
        else {
            current = numNames[number % 10]+" ";
            number /= 10;
            
            current = tensNames[number % 10]+" " + current;
            number /= 10;
        }
        if (number == 0) return current;
        return numNames[number] + " hundred " + current;
    }
    
    public static String convert(int number) {

        if (number == 0) { return "zero "; }
        
        String prefix = "";
        
        if (number < 0) {
            number = -number;
            prefix = "negative ";
        }
        
        String current = "";
        int place = 0;
        
        do {
            int n = number % 1000;
            if (n != 0){
                String s = convertLessThanOneThousand(n);
                current = s+ specialNames[place]+" " + current;
            }
            place++;
            number /= 1000;
        } while (number > 0);
        
        return (prefix + current).trim();
    }

- KD March 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Main {

static String[] numNames={"","one","two","three","four","five","six","seven","eight","nine","ten","eleven",
"twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"};
static String[] tensNames={"","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
static String[] specialNames={"","thousand","million","billion","trillion","quadrillion","quintillion"};

public static void main(String[] args) {

System.out.println("======"+convert(1000003));

}
public static String convertLessThanOneThousand(int number) {
String current;

if (number % 100 < 20){
current = numNames[number % 100]+" ";
number /= 100;
}
else {
current = numNames[number % 10]+" ";
number /= 10;

current = tensNames[number % 10]+" " + current;
number /= 10;
}
if (number == 0) return current;
return numNames[number] + " hundred " + current;
}

public static String convert(int number) {

if (number == 0) { return "zero "; }

String prefix = "";

if (number < 0) {
number = -number;
prefix = "negative ";
}

String current = "";
int place = 0;

do {
int n = number % 1000;
if (n != 0){
String s = convertLessThanOneThousand(n);
current = s+ specialNames[place]+" " + current;
}
place++;
number /= 1000;
} while (number > 0);

return (prefix + current).trim();
}
}

- KD March 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.pli.project.algorithm.ms;

import java.util.HashMap;

/**
 * Created by lipeng on 2016/3/18.
 * Given big writing of a number. Convert it to integer.
 */
public class GetNumber {

    public static HashMap<String, Integer> numberMap;

    static {

    }

    public GetNumber() {
        if (numberMap == null) {
            numberMap = new HashMap<>();
            numberMap.put("one", 1);
            numberMap.put("two", 2);
            numberMap.put("three", 3);
            numberMap.put("four", 4);
            numberMap.put("five", 5);
            numberMap.put("six", 6);
            numberMap.put("seven", 7);
            numberMap.put("six", 8);
            numberMap.put("nine", 9);
            numberMap.put("ten", 10);
            numberMap.put("eleven", 11);
            numberMap.put("twelve", 12);
            numberMap.put("thirteen", 13);
            numberMap.put("fourteen", 14);
            numberMap.put("fifteen", 15);
            numberMap.put("sixteen", 16);
            numberMap.put("seventeen", 17);
            numberMap.put("eighteen", 18);
            numberMap.put("nineteen", 19);
            numberMap.put("twenty", 20);
            numberMap.put("thirty", 30);
            numberMap.put("forty", 40);
            numberMap.put("fifty", 50);
            numberMap.put("sixty", 60);
            numberMap.put("seventy", 70);
            numberMap.put("eighty", 80);
            numberMap.put("ninety", 90);
            numberMap.put("hundred", 100);
            numberMap.put("", 0);
        }
    }

    public int getNumber(String str) {
        String[] arr = str.split(" ");
        int num = 0;
        int ans = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].contains("hundred")) {
                num = num * 100;
            }
            else if (arr[i].contains("thousand")) {
                ans = ans + num * 1000;
                num = 0;
            }
            else if (arr[i].contains("million")) {
                ans = ans + num * 1000000;
                num = 0;
            }
            else if (arr[i].contains("billion")) {
                ans = ans + num * 1000000000;
                num = 0;
            }
            else {
                num += numberMap.get(arr[i]);
            }
        }
        return ans + num;
    }

    public static void main(String[] args) {
        GetNumber getNumber = new GetNumber();
        System.out.println(getNumber.getNumber("one hundred thousand"));
        System.out.println(getNumber.getNumber("one million two hundred"));
        System.out.println(getNumber.getNumber("one million two hundred thousand"));
        System.out.println(getNumber.getNumber("one million two hundred thousand fifty seven"));
        System.out.println(getNumber.getNumber("one million two hundred thousand two hundred fifty seven"));
        System.out.println(getNumber.getNumber(""));
        System.out.println(getNumber.getNumber("one million one"));
    }

}

- allen.lipeng47 March 19, 2016 | 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