IBM | C++ timer team

round 1, 16 Sept

  1. Difference b/w these two. How memory is stored for them?
	char a[] = "indian";
	char *a = "indian";
  1. static variable, how to use them
  2. lvalue reference, diff b/w pointer and reference
  3. sizeof is operator or macro. (he said macro). Implement sizeof which works on types too. not just variables
  4. write prog to delete occurance of char ch in a string. dont use library functions. here I used one
	void deleteChar(string &str, char ch) { 
	  //int len = str.length();
	  int len = strlen(str.c_str());
	  int idx = 0;
	  for(int i=0; i<len; i++) {
		if(str[i] !=ch) {
		  str[idx++] = str[i];
		}
	  }
	  //cout << idx << endl;
	  str = str.substr(0, idx);
	}
  1. what this code is doing. (Selection sort)
void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}
void myFunc(int array[], int size) {
  for (int step = 0; step < size - 1; step++) {
    int min_idx = step;
    for (int i = step + 1; i < size; i++) {
      if (array[i] < array[min_idx])
        min_idx = i;
    }
    swap(&array[min_idx], &array[step]);
  }
}

modify this such that it sort as per the largest number possible with the array elements
I did like this: (worked fine)

void myFunc(int array[], int size) {
  for (int step = 0; step < size - 1; step++) {
    int min_idx = step;
    for (int i = step + 1; i < size; i++) {
      string a = to_string(array[i]);
      string b = to_string(array[min_idx]);
      if((a+b) > (b+a)) {
        min_idx = i;
      }
      //if (array[i] < array[min_idx])
       // min_idx = i;
    }
    swap(&array[min_idx], &array[step]);
  }
}
  1. gave one code where base class has pure virtual function and derived class was not overriding it. And he was doing like this
base *obj = new Derived();
  • asked to find bug
  • issue is since the derived class also became abstract its obj cant be created now.
  • fix it to override the virtua funct in drvd class
  1. Virtual functions
  2. asked about multi threading. when to use it.
  3. lots of behavioural questions
  4. why you want to leave
  5. whats your biggest achievement so far
Comments (1)