Thursday, November 24, 2016

C++ STL questions

What are different exception safe containers?
Which is more exception safe list or vector?
Name all standard containers?
How do u sort a list(std::sort requires random access iterators, hence will not work)?
When to use list and vector? (Answer should address complexity, vector contigueness, exception safety and stability of iterators)
What is the last compiler used?
What are weak pointers for?
Why does one use virtual inheritence and private inheritence?



  • Why and when do you need copy constructors? Is there any situation where you want to only implement a copy constructor or an assignment operator but not both?
  • Can you use virtual functions in constructors and destructors? What's going to happen if you try? Why? (this is how it's implemented is unacceptable)
  • If you have a virtual function, do you have to have virtual destructor? Why?
  • Write a code to show "pure virtual function call" runtime error.
  • What is a template? What is template instantiation? What happens if you have a static field in a template class? How does it all work with the linker?
  • Describe how objects are implemented. What is the implementation of virtual functions? How come the compiled code works in descendants? What about multiple inheritance?
  • Why can't you throw exceptions from a destructor?
  • Describe virtual inheritance (it's not the same as virtual functions!)
  • Can you have an array of references? Why not? (this is forbidden by the language is unacceptable)

Tuesday, November 15, 2016

Linked list problems

Here the list of questions about linked list

typedef struct node
{
     struct node* link ;
     int data ;
}node ;

static void reverse(node** head)
{
     node* cur = *head ;
     node* prev = NULL ;
     node* next = cur->next ;
     while (cur->next != NULL)
     {
             next = cur->next ;
             cur->next = prev ;
             prev = cur ;
             cur = next ;
       }
     *head = prev ;
}


// Remove nth node from list
void remove_nth_from_last(node** head, int n)
{
       node* cur = head ;
       while (n--)
           cur = cur->next ;
      node* slow = head ;
      while (cur->next != NULL)
      {
            cur = cur->next ;
            slow = slow ->next ;
       }

   // slow now points the node pointing from last nth node
   node* temp = slow ;
   temp->next = slow ->next ;
   temp->data = slow->next->data ;
   free(slow) ;
}

node* merge_sorted_list(node* l1, node* l2)
{
    node head = (node*) getnode(0) ;
    node* tail = &head ;
    while (l1->next != NULL && l2->next != NULL)
    {
            if (l1->val < l2->val)
           {
                 tail ->next = getnode(l1->val) ;
                 l1 = l1->next ;
            }
            else
            {
                tail->next = getnode(l2->val) ;
                 l2 = l2->next ;
            }
            tail = tail->next ;
  }
  if (l1->next)?tail->next = l1 : tail->next = l2 ;
  return head->next ;
}

// return
// 0 -- loop not found
// 1 -- loop found
int detect_loop(node* head)
{
    if (head == NULL)
           return 0 ;
   node* slow = head, *fast = head ;
   while (fast->link != NULL)
   {
              if (fast->link == NULL)
                       return 1 ;
              if (slow == fast)
                    return 1 ;
              fast = fast->link ;
              if (slow == fast)
                    return 1 ;
             slow = slow->next ;
             fast = fast->next ;
    }
    return 0;
}

      
             

           

Monday, November 14, 2016

string functions

int myStrcmp(const char* source, const char* dest)
{
       while (*source == *destination)
       {
              if (*source == '\0')
                    return 0 ;
              source++, destination-- ;
       }
       return (*source - *destination) ;
}

void myStrcpy(const char* src, char* dest)
{
        while (*dest++ = *src++) ;
}

char* myStrcat(const char* src, char* dst)
{
    int c=0, a=0;
    while (dst[c] != '\0)
        c++ ;
    for (int a=0; a<strlen(src); a++)
       dst[c+a] = src[a] ;
   dst[c+a] = '\0' ;
   return dst ;
}

void myMemcpy(void *src, void* dst, size_t len)
{
     char * s = (char*) src ;
     char * d = (char*) dst ;
     for (int i = 0; i<len; i++)
          s[i] = d[i] ;
 }


Thursday, August 18, 2016

Reverse string

#include <string.h>
void reverse(char a[])
{
         int i, j,
         int j = strlen(a) -1 ;
         for (i=0; i<j; i++, j--)
         {
                  c = a[i] ;
                  a[i] = a[j] ;
                  a[j] = c ;
        }
}

Print the number in binary format

#include <stdio.h>
int main()
{
       int a  = 10 ;
       printf("Enter a number to print in binary format\n") ;
       scanf("%d", &a) ;
       while (a)
       {
               if (a&1 == 1)
                       printf("1") ;
               else
                      printf("0") ;
               a  = a>>1 ;
       }
       printf("\n") ;
}

Swap bits for given character

Swap first 4 bits of a number to next 4 bits
Example :
1011 0011 to 0011 1011

Get first 4 bits : (Number & 0XF0), shift 4 positions to right (number & 0Xf0) >> 4

Get the last 4 bits : number & 0XOF, shift 4 position to left, number & 0XOF) << 4

Return sum (OR) of above is the swapped number

unsigned char c ;   // input number
c = (c & 0XF0) >> 4 | (c &0X0F) << 4)

Friday, August 12, 2016

C Program to print even and odd numbers using threads

Save file as PrintEvenOdd.c and compile as
gcc PrintEvenOdd.c -lpthread
#include<stdio.h>
#include<pthread.h>

pthread_t tid[2];
unsigned int shared_data = 0;
pthread_mutex_t mutex;
unsigned int rc;
//prototypes for callback functions

void* PrintEvenNos(void*);
void* PrintOddNos(void*);

void main(void)
{
    pthread_create(&tid[0],0,&PrintEvenNos,0);
    pthread_create(&tid[1],0,&PrintOddNos,0);
    sleep(3);

    pthread_join(tid[0],NULL);
    pthread_join(tid[1],NULL);
}

void* PrintEvenNos(void *ptr)
{
     rc = pthread_mutex_lock(&mutex);
     do
     {
         if(shared_data%2 == 0)
         {
             printf("Even:%d\n",shared_data);
             shared_data++;
         }
         else
         {
             rc=pthread_mutex_unlock(&mutex);//if number is odd, do not print, release mutex
         }
     } while (shared_data <= 100);
}

void* PrintOddNos(void* ptr1)
{
    rc = pthread_mutex_lock(&mutex);
    do
    {
        if(shared_data%2 != 0)
        {
            printf("odd:%d\n",shared_data);
            shared_data++;
        }
        else
        {
            rc = pthread_mutex_unlock(&mutex);//if number is even, do not print, release mutex
        }
    } while (shared_data <= 100);
}