TeachingBee

IBM Coding Questions 2022

IBM Coding Questions

IBM is one of the top companies and leading recruiters. If you are planning to bag job in IBM and wondering how to prepare for IBM coding questions, here we are to guide you through.In this blog, we’ve discussed the sample IBM coding questions which are likely to be asked in IBM.

IBM Coding Questions

Q1. Write a program to find HCF of two numbers by without using recursion

Examples:

Input: x = 16, y = 32 
Output: 16

Input: x = 12, y = 15 
Output: 3 

Algorithm:

  1. Check if smaller of two number divide larger number. If yes, HCF is smaller
  2. Else, Start from smaller/2 to 1 and check if current number divides both the input numbers.
  3. If yes, then current number is required HCF.

Implementation:

// IBM Coding Questions: HCF of Two numbers without recursion
#include <iostream>

using namespace std;

int findHCF(int x, int y) {

  // Find minimum of two numbers
  int smallest = min(x, y);
  // If both numbers divisible by smallest number,
  // then smallest is the HCF.
  if (x % smallest == 0 && y % smallest == 0)
    return smallest;

  // Loop through smallest/2 to 1 and
  // check if number divides both input numbers
  for (int i = smallest / 2; i >= 2; i--) {
    if (x % i == 0 && y % i == 0)
      return i;
  }

  // If no number divides then HCF is 1.
  return 1;
}

int main() {
  int x = 16, y = 32;
  cout << findHCF(x, y);

  return 0;
}

Q2. Write a program to find the sum of A.P series

Examples:

Input : a = 1
        d = 2
        n = 4
Output : 16
1 + 3 + 5 + 7 = 16

Algorithm:

Sum of arithmetic series is ((n / 2) * (2 * a + (n - 1) * d))
           Where
               a is First term
               d is Common difference
               n is No of terms

Implementation:

// IBM Coding Questions: Find the sum of A.P series

#include<bits/stdc++.h>
using namespace std;
 
// Function to find sum of series.
float sumOfAP(float a, float d, int n)
{
    float sum = 0;
    for (int i=0;i<n;i++)
    {
        sum = sum + a;
        a = a + d;
    }
    return sum;
}
 
int main()
{
    int n = 4;
    float a = 1, d = 2;
    cout<<sumOfAP(a, d, n);
    return 0;
}

Q3. Write a program to Change Decimal Number to Binary

Examples:

Input : 7
Output : 111

Input : 10
Output : 1010

Input: 33
Output: 100001

Algorithm:

  1. Keep dividing number by 2 until it becomes 0.
  2. The remainder obtained on dividing on each run is stored and converted to right format to return.

Implementation:

// IBM Coding Questions: Change Decimal Number to Binary

#include <stdio.h>

#include <math.h>


long long decimalToBinary(int n) {
  long long ans = 0;
  int rem, i = 1;

  // loop until 0
  while (n != 0) {
    // get remainder
    rem = n % 2;
    n /= 2;
    ans += rem * i;
    i *= 10;
  }
  return ans;
}

int main() {
  int n = 10, ans;
  printf("Enter a decimal number: ");
  ans = decimalToBinary(n);
  printf("%d in decimal =  %lld in binary", n, ans);
  return 0;
}

Q4. Write a program to generate Fibonacci Triangle

Examples:

Input : n = 5 
Output :
1 
1 2 
3 5 8 
13 21 34 55 
89 144 233 377 610 

Algorithm:

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation 

    Fn = Fn-1 + Fn-2.   where F1 = 1 and F2 = 1.

Implementation:

// IBM Coding Questions: program to generate Fibonacci Triangle

#include <bits/stdc++.h>

using namespace std;

void fib(int f[], int N) {
  f[1] = 1;
  f[2] = 1;

  for (int i = 3; i <= N; i++)

    // Add the previous 2 numbers and store it
    f[i] = f[i - 1] + f[i - 2];
}

void fibTriangle(int n) {
  // To make triangle of height n total elements needed are n*(n+1)/2 
  int N = n * (n + 1) / 2;
  int f[N + 1];
  fib(f, N);
  int count = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++)
      cout << f[count++] << " ";
    cout << endl;
  }
}

int main() {
  int n = 5;
  fibTriangle(n);
  return 0;
}

Q5. Write a program to remove the vowels from the input string

Examples:

Input : prepare for interviews on teachingbee
Output : prpr fr ntrvws n tchngb

Algorithm:

  1. Traverse through sentence
  2. If character is not a,e,i,o,u r store character in new string else ignore.

Implementation:

// IBM Coding Questions: remove the vowels from the input string

#include <bits/stdc++.h>

using namespace std;

string removeVowels(string str) {
  vector < char > vowels = {
    'a',
    'e',
    'i',
    'o',
    'u',
    'A',
    'E',
    'I',
    'O',
    'U'
  };

  for (int i = 0; i < str.length(); i++) {
    // Check if character is vowel or not.
    if (find(vowels.begin(), vowels.end(),
        str[i]) != vowels.end()) {
      str = str.replace(i, 1, "");
      i -= 1;
    }
  }
  return str;
}

int main() {
  string str = "prepare for interviews on teachingbee";
  cout << removeVowels(str) << endl;

  return 0;
}

Q6. Write a program to find string is palindrome or not

Examples:

Input: abba
Output: abba is a palindrome

Algorithm:

  1. Get the length of input string. 
  2. Do following while low index ‘l’ is smaller than high index ‘h’ where l is initialised to 0 and high to n-1
    • If str[l] is not same as str[h], then return false. 
    • Increment l and decrement h, i.e., do l++ and h–. 
  3. If all character match, return true.

Implementation:

// IBM Coding Questions: program to find string is palindrome or not
#include <stdio.h>

#include <string.h>

void isPalindrome(char str[]) {
  int l = 0;
  int h = strlen(str) - 1;

  // Keep comparing characters till characters match
  while (h > l) {
    if (str[l++] != str[h--]) {
      printf("%s is not a palindrome\n", str);
      return;
    }
  }
  printf("%s is a palindrome\n", str);
}

int main() {
  isPalindrome("abba");
  return 0;
}

Conclusion

In this blog, we’ve discussed the sample IBM coding questions which are likely to be asked in IBM.

In this article we discussed question asked in various aptitude exams of various companies. Checkout Other commonly asked questions here

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Rust for AI-teachingbee

Is Rust Good for AI?

Artificial Intelligence (AI) has revolutionised various industries, from healthcare to finance, by enabling machines to perform tasks that typically require human intelligence. As the demand for AI solutions continues to

salary of Rust programmer in India-teachingbee

What is the salary of Rust programmer in India?

Rust is a modern, statically typed programming language designed for performance, safety, and concurrency. Developed by Mozilla, Rust offers memory safety without a garbage collector, making it suitable for systems

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In