IBM Coding Questions 2022

 

IBM Coding Questions 2022





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.




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



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.


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.




Algorithm:

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



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.




// 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;
}

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...