HomeIBM Off CampusIBM coding questions with answers 2023 | IBM hackerrank coding questions

IBM coding questions with answers 2023 | IBM hackerrank coding questions

- Advertisement -
Telegram Group Join Now
Instagram Page Join Now
4/5 - (4 votes)

IBM coding questions with answers 2023 2024 | IBM hackerrank coding questions : If you are looking for IBM previous year question or this year then you land on right page. Check below IBM Coding question which was asked in November 2023 Exam.

Problem Statement:

When we share our phone numbers with others, we share the numbers digit by digit.

Given a phone number in words, convert the number to digits.
For example, convert “six four eight three” to “6483”.

  1. All phone numbers will be 10 digits.
  2. Two repeating digits will be shortened using the word “double”. Three repeating
    digits will be shortened using the word “triple”. If the digits repeat four or more
    times, they will be shortered using ‘double’ and ‘triple’ mutiple times.

Check Other IBM Question Here: Question Link

IBM Latest Interview Experience: Check here

Solution:

// Tech program mind
// Use this editor to write, compile and run your Java code online
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.*;

class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String str = sc.nextLine();
String [] arr = str.split(" ");
String ans = "";
int i=0;
HashMap wordToDigitMap = new HasMap<>();
wordToDigitMap.put("zero", "0");
wordToDigitMap.put("one", "1");
wordToDigitMap.put("two", "2");
wordToDigitMap.put("three", "3");
wordToDigitMap.put("four", "4");
wordToDigitMap.put("five", "5");
wordToDigitMap.put("six", "6");
wordToDigitMap.put("seven", "7");
wordToDigitMap.put("eight", "8");
wordToDigitMap.put("nine", "9");
while(i<arr.length){
if(arr[i].equals("double")){
ans= ans+wordToDigitMap.get(arr[i+1]) + wordToDigitMap.get(arr[i+1]);
i++;
}
else if(arr[i].equals("triple")){
ans= ans+wordToDigitMap.get(arr[i+1]) + wordToDigitMap.get(arr[i+1]) + wordToDigitMap.get(arr[i+1]);
i++;
}
else {
ans= ans+wordToDigitMap.get(arr[i]);
}
i++;
}
System.out.print(ans);

}
}

IBM Code Explanation YouTube Video:

IBM Coding Question with answer

For More IBM Coding Question Join us:

Join Telegram (Must)Click Here
Join Experienced Job TelegramClick Here
Follow Instagram Job Page Link:Join Here
RELATED ARTICLES

9 COMMENTS

  1. program for convertin phone number words into number ?

    code i have tried in python

    dic = {
    ‘zero’:’0′,
    ‘one’:’1′,
    ‘two’:’2′,
    ‘three’:’3′,
    ‘four’:’4′,
    ‘five’:’5′,
    ‘six’:’6′,
    ‘seven’:’7′,
    ‘eight’:’8′,
    ‘nine’:’9′
    }
    phn = ‘five one zero six nine six four triple eight’
    singal = phn.split(‘ ‘)
    for i in range(len(singal)):
    if singal[i] == ‘double’:
    var = singal[i+1]
    print(dic[var],end=”)
    continue
    if singal[i] == ‘triple’:
    var = singal[i + 1]
    print(dic[var] * 2, end=”)
    continue

    print(dic[singal[i]],end=”)

  2. #include
    #include
    #include

    int main() {
    std::string input;
    std::getline(std::cin, input);

    std::istringstream iss(input);
    std::string word;

    std::unordered_map wordToDigitMap;
    wordToDigitMap[“zero”] = “0”;
    wordToDigitMap[“one”] = “1”;
    wordToDigitMap[“two”] = “2”;
    wordToDigitMap[“three”] = “3”;
    wordToDigitMap[“four”] = “4”;
    wordToDigitMap[“five”] = “5”;
    wordToDigitMap[“six”] = “6”;
    wordToDigitMap[“seven”] = “7”;
    wordToDigitMap[“eight”] = “8”;
    wordToDigitMap[“nine”] = “9”;

    std::string result = “”;
    while (iss >> word) {
    if (word == “double”) {
    result += wordToDigitMap[iss.str()] + wordToDigitMap[iss.str()];
    iss >> word; // Skip the next word as it was already processed
    } else if (word == “triple”) {
    result += wordToDigitMap[iss.str()] + wordToDigitMap[iss.str()] + wordToDigitMap[iss.str()];
    iss >> word; // Skip the next word as it was already processed
    } else {
    result += wordToDigitMap[word];
    }
    }

    std::cout << result;

    return 0;
    }

  3. i have tried in python, here is the compact solution:

    def texttono(text, no):
    txt = text.split(‘ ‘)
    for i in txt:
    if i in no:
    print(no[i])

    no = {
    ‘one’ : 1,
    ‘two’ : 2,
    ‘three’ : 3,
    ‘four’ : 4,
    ‘five’ : 5,
    ‘six’ : 6,
    ‘seven’ : 7,
    ‘eight’ : 8,
    ‘nine’ : 9,
    ‘zero’ : 0
    }

    text = “six three six zero three nine three two four four ”
    texttono(text, no)

  4. class Greeter:

    def covert(self, s: str ) -> str :
    mydict = {
    “zero” : “0”,
    “one” : “1”,
    “two” : “2”,
    “three” : “3”,
    “four” : “4”,
    “five” : “5”,
    “six” : “6”,
    “seven” : “7”,
    “eight” : “8”,
    “nine” : “9”
    }
    final = “”
    s = s.split()
    i = 0
    while i < len(s):
    if s[i] not in ["double", "triple"]:
    final = final + mydict[s[i]]
    i = i+1
    elif i+1 < len(s) and s[i] == "double":
    final = final + "" + mydict[s[i+1]]+ mydict[s[i+1]]
    i = i+2
    elif i+1 < len(s) and s[i] == "triple":
    final = final + "" + mydict[s[i+1]]+ mydict[s[i+1]]+ mydict[s[i+1]]
    i = i+2
    print("Result—–", final)

    greeter = Greeter()
    greeter.covert("eight five triple two four nine five triple six")
    greeter.covert("eight five double two double two four nine five six")

  5. import java.util.*;
    class prgm{
    static void mymethod( String str1)
    {
    if(str1.equals(“nine”))
    {
    System.out.print(“9”);
    }
    else if(str1.equals(“eight”))
    {
    System.out.print(“8”);
    }
    else if(str1.equals(“seven”))
    {
    System.out.print(“7”);
    }
    else if(str1.equals(“six”))
    {
    System.out.print(“6”);
    }
    else if(str1.equals(“five”))
    {
    System.out.print(“5”);
    }
    else if(str1.equals(“four”))
    {
    System.out.print(“4”);
    }
    else if(str1.equals(“three”))
    {
    System.out.print(“3”);
    }
    else if(str1.equals(“two”))
    {
    System.out.print(“2”);
    }
    else if(str1.equals(“one”))
    {
    System.out.print(“1”);
    }
    else if(str1.equals(“zero”))
    {
    System.out.print(“0”);
    }

    }
    public static void main(String arg[]){
    Scanner s= new Scanner(System.in);
    //declaring and initializing a string
    String str = “Nine eight seven six double four triple two zero”;
    str=str.toLowerCase();
    //declaring an empty string array
    String[] strArray = null;
    //converting using String.split() method with whitespace as a delimiter
    strArray = str.split(” “);
    //printing the converted string array
    for (int i = 0; i< strArray.length; i++)
    {
    //System.out.println(strArray[i]);
    if(strArray[i].equals("double"))
    {
    mymethod(strArray[i+1]);

    }
    else if(strArray[i].equals("triple"))
    {
    mymethod(strArray[i+1]);
    mymethod(strArray[i+1]);

    }
    else
    {
    mymethod(strArray[i]);
    }
    }
    }
    }

  6. static void sol(String input)
    {
    String str=””;
    int i,j,k;
    String arr[]=input.split(” “);
    for(i=0;i<arr.length;i++)
    { j=1;
    if (arr[i].equals("double") )
    {
    j=2;
    i++;
    }else if(arr[i].equals("triple"))
    {
    j=3;
    i++;
    }
    for(k=0;k<j;k++)
    {
    switch(arr[i])
    {
    case "one":
    str=str+"1";
    break;
    case "two":
    str=str+"2";
    break;
    case "three":
    str=str+"3";
    break;
    case "four":
    str=str+"4";
    break;
    case "five":
    str=str+"5";
    break;
    case "six":
    str=str+"6";
    break;
    case "seven":
    str=str+"7";
    break;
    case "eight":
    str=str+"8";
    break;
    case "nine":
    str=str+"9";
    break;
    }

    }

    }

    System.out.println(str);
    }

  7. String n = “Five eight double two double two four eight five six”;

    String[] arr = {“zero”,”one”,”two”,”three”,”four”,”five”,”six”,”seven”,”eight”,”nine”};

    String[] split_arr = n.split(” “);

    int count = 0;
    for(int i = 0; i < split_arr.length; i++){
    if(split_arr[i].equals("double")){
    count += 2;
    continue;
    }
    if(split_arr[i].equals("triple")){
    count += 3;
    continue;
    }

    for(int k = 0; k 0){
    for (int j = 0; j < count; j++){
    System.out.print(k);
    }
    break;
    }
    System.out.print(k);
    }
    }
    count = 0;
    }

  8. public class Main {

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    String[] arr=s.split(” “);
    HashMap map=new HashMap();
    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’);
    String ans=””;
    for(int i=0;i<arr.length;i++) {
    if(arr[i].equals("double")) {
    ans=ans+map.get(arr[i+1]);
    }
    else if(arr[i].equals("triple")){
    ans=ans+map.get(arr[i+1])+map.get(arr[i+1]);
    }
    else {
    ans=ans+map.get(arr[i]);
    }
    }
    System.out.println(ans);

    }
    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular