7/15/08

General Programming Interview Questions

1.

How many problems can you spot in the following class definition?

class MyString()

{

MyString()

{

}

~MyString()

{

delete m_pText;

}

void Set(char* pText)

{

size_t length = strlen(pText);

m_pText = new char[length];

}

private:

m_pText;

}

int _tmain(int argc, _TCHAR* argv[])

{

return 0;

}

  1. Class declaration is wrong ended with ().
  2. Type for m_pText is not specified.
  3. Class declaration closing brace is not ended with semicolon
  4. m_pText should be deleted as delete[] and not just delete.
  5. you cant take the strlen of pointer as if it is not null terminated one it will give wrong results.

2.

Explain the difference between the following...

- public - These members can be accessed any where, inside class outside class in derived class.

- protected – can be used within the class and inside derived classes.

- private - can be used only inside the class,

- mutable – if a const method is declared which ensure that the state of object or member variable will not be modified inside such const method . one can declare the variable as mutable and it modified by const method. Basically used for const ojbects.

3.

Write a template function to swap two values of the same type.

template <class type> void swap( type & t1, type & t2)

{

type t3 = t1;

t1= t2;

t2 = t3;

}

4.

Declare a structure which could be used as a single node in a double linked list. The node will store one integer and a pointer to a long.

struct NODE {

Node * next;

long * ptr;

int data;

};

5.

Given the following definitions:

#define STYLE_MINIMIZE 0x01

#define STYLE_MAXIMIZE 0x02

#define STYLE_CLOSE 0x04

#define STYLE_ON 0x08

1. Write a function which takes an unsigned integer and a set of style flags. The function should return true if all flags are set.

Struct {

Bool Func( unsigned int arg1, int style)

{

Return ( (arg1 & STYLE_MINIMIZE) && (arg1 & 0x02) &&(arg1 & 0x03)&& (arg1 & 0x04))? True : false;

}

2. Write a function which takes a reference to an unsigned integer and a set of style flags. The function should add the flags if they don't exist and remove them if they do.

3. Define an unsigned integer variable with both the minimize and maximize style.

4. Add the close style.

5. Use the method from #2 t o add and remove the ON style.

6.

How would you reverse the words in a string? E.g., “the quick brown fox” -> “fox brown quick the”

Void StringReverse( char * ptr)

{

Int I =0;

Whil(ptr[i] != ‘\0’) i++;

Char *temp = new char[i];

Int j =I;

While( i>0)

{

Temp[j-i] = ptr[i];

}

Strcpy(ptr, temp);

}

7.

How would you count the number of bits switched on in a number?

1. operate with ‘&’ operation on the specific numbered bit.

int bitcount (unsigned int n)

{

int count=0;

int counter =0;

while (n)

{

count += n & 0x1u ;

n >>= 1 ;

counter++;

}

return count ;

}

8.

What is wrong with the following code?

void CopyTheString(TCHAR *sourceString, TCHAR *destString)
{
size_t lengthOfSource = _tcslen(sourceString);
TCHAR *tempPtr = (TCHAR *) malloc(lengthOfSource);
memmove(tempPtr, sourceString, lengthOfSource);
destString = tempPtr;
}

9.

How would you reverse a singly linked list?

You need to keep two extra pointer to hold the pointers

10.

There is an array of sorted items. An arbitrary element is picked in this array and the portion of the array starting at that element is moved to the front and the portion of the array before this element is pushed further down. What is the best algorithm (asymptotically) for finding an element in this new modified array?

Binary search

11.

X=a, Y=b. How would you swap the values in X and Y without using additional variables?

x= x+y;

y = x-y;

x= x - y

12.

Why would you not want to throw an exception from a destructor?

The memory allocated after the throw statement will not be freed forever.

No comments:

ITUCU