Interview Question


Country: Bangladesh
Interview Type: Written Test




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

Create an array of strings 0 - 9. Pick out digits using %. Build an output string using these digits.

- Adrian November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code may helps you

int n=1234;
int rev=0;int d;
while(n!=0){
d=n%10;
rev=rev*10+d;
n=n/10;
}

int num=rev;
while(num!=0){
d=num%10;
switch(d){
case 1:System.out.println("one");break;
case 2:System.out.println("two");break;
case 3:System.out.println("three");break;
case 4:System.out.println("four");break;
case 5:System.out.println("five");break;
case 6:System.out.println("six");break;
case 7:System.out.println("seven");break;
case 8:System.out.println("eight");break;
case 9:System.out.println("nine");break;
}
num=num/10;
}

}

- yam kolli November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Map <String, String> map = new HashMap<String, String>();
map.put("0", "Zero");
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("4", "Four");
map.put("5", "Five");
map.put("6", "Six");
map.put("7", "Seven");
map.put("8", "Eight");
map.put("9", "Nine");
char[] arr = input.toCharArray();
StringBuilder builder = new StringBuilder();
for(char a: arr){
builder.append(map.get(Character.valueOf(a).toString()));
builder.append(" ");
}
return builder.toString();

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

pls help me to write the code in java without using built in method.

- maria November 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this code may helps to you



public class NumericToWords {
public static void main(String[] args) {
int n=1234;
int rev=0;int d;
while(n!=0){
d=n%10;
rev=rev*10+d;
n=n/10;
}

int num=rev;
while(num!=0){
d=num%10;
switch(d){
case 1:System.out.println("one");break;
case 2:System.out.println("two");break;
case 3:System.out.println("three");break;
case 4:System.out.println("four");break;
case 5:System.out.println("five");break;
case 6:System.out.println("six");break;
case 7:System.out.println("seven");break;
case 8:System.out.println("eight");break;
case 9:System.out.println("nine");break;
}
num=num/10;
}

}
}

- yam kolli November 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# ruby : convert numbers to equivalent words
def to_words(n)
  words = %w(zero one two three four five six seven eight nine ten)
  result = (n.each_char.map { |c| words[c.to_i] }).join " "
end
puts to_words("2490")

- sean December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printNumber(String s){
		for(int i = 0;i < s.length();i++){
			System.out.print(map.get(s.substring(i, i +1)));
			System.out.print(" ");
		}
	}
	public static void fillMap(){
		    map = new HashMap<String, String>();
			map.put("0","zero");
			map.put("1","one");
			map.put("2","two");
			map.put("3","three");
			map.put("4","four");
			map.put("5","five");
			map.put("6","six");
			map.put("7","seven");
			map.put("8","eight");
			map.put("9","nine");
		
	}
	public static HashMap<String, String> map;

- Ahmad December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Enum instead, see the below code
package exam;

public enum word
{
ZERO(0,"ZERO"),
ONE(1,"ONE"),
TWO(2,"TWO"),
THREE(3,"THREE"),
FOUR(4,"FOUR"),
FIVE(5,"FIVE");

private int digit;
private String digitWord;

word(int digit, String digitWord)
{
this.digit = digit;
this.digitWord = digitWord;
}

public static String lookup(int digit)
{
for(word w : word.values())
{
if(w.getDigit() == digit)
{
return w.getDigitWord();
}
}
return null;
}
public int getDigit()
{
return digit;
}
public void setDigit(int digit)
{
this.digit = digit;
}
public String getDigitWord()
{
return digitWord;
}
public void setDigitWord(String digitWord)
{
this.digitWord = digitWord;
}
}





package exam;

public class exercise1
{
public static void main(String args[])
{

String convertDigitToWord = word.lookup(5);
System.out.println(convertDigitToWord);
}
}

- Anonymous December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You don't need a hashmap since you can use the array index to get the word.
Here's my code

public class Main {

    public static void main(String[] args) {
      printNumberAsWords("2490");
    }

    private static void printNumberAsWords(String inputString) {
        List<String> numbersAsWords = Arrays.asList("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
        String result="";
        for(Character c : inputString.toCharArray()){
            int digit = Character.digit(c, 10);
            result+=numbersAsWords.get(digit)+" ";
        }
        System.out.println(result);
    }


}

- gopal.vandana December 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If i wantt to write 2490 as "two thousands four hundreds ninty".
what is the code????

- shubhi January 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.test.me;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class printNumbers {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//BufferedReader bufRead = new BufferedReader(new InputStreamReader(System.in));
int fnum= 6345;
//System.out.println(fnum);
int value = 1;
ArrayList<Integer> tmp = new ArrayList<>();
int length = String.valueOf(fnum).length();
//System.out.println(length);
tmp.add(1);
for(int i=0;i<length-1;i++){
value = value*10;
// System.out.println(value);
tmp.add(value);
}
Collections.reverse(tmp);
for(Integer i:tmp){
// System.out.println(tmp);
int num =fnum/i;
// System.out.println(num);
switch (num) {
case 1:
System.out.print("One");
break;
case 2:
System.out.print("Two");
break;
case 3:
System.out.print("Three");
break;
case 4:
System.out.print("Four");
break;
case 5:
System.out.print("Five");
break;
case 6:
System.out.print("Six");
break;
default:
break;
}
fnum = fnum-(i*num);

// System.out.println(num);

}


}

}

- SkullCrusher February 24, 2013 | 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