AdBrite

Your Ad Here

Thursday, April 21, 2011

What is the difference between TCP and UDP

Difference between TCP and UDPThere are two types of internet protocol (IP) traffic, and both have very different uses.
  1. TCP(Transmission Control Protocol). TCP is a connection-oriented protocol, a connection can be made from client to server, and from then on any data can be sent along that connection.
    • Reliable - when you send a message along a TCP socket, you know it will get there unless the connection fails completely. If it gets lost along the way, the server will re-request the lost part. This means complete integrity, things don't get corrupted.
    • Ordered - if you send two messages along a connection, one after the other, you know the first message will get there first. You don't have to worry about data arriving in the wrong order.
    • Heavyweight - when the low level parts of the TCP "stream" arrive in the wrong order, resend requests have to be sent, and all the out of sequence parts have to be put back together, so requires a bit of work to piece together.
  2. UDP(User Datagram Protocol). A simpler message-based connectionless protocol. With UDP you send messages(packets) across the network in chunks.
    • Unreliable - When you send a message, you don't know if it'll get there, it could get lost on the way.
    • Not ordered - If you send two messages out, you don't know what order they'll arrive in.
    • Lightweight - No ordering of messages, no tracking connections, etc. It's just fire and forget! This means it's a lot quicker, and the network card / OS have to do very little work to translate the data back from the packets.

IUnknown Interface


Enables clients to get pointers to other interfaces on a given object through the QueryInterface method, and manage the existence of the object through the AddRef and Release methods. All other COM interfaces are inherited, directly or indirectly, from IUnknown. Therefore, the three methods in IUnknown are the first entries in the VTable for every interface.
When To Implement

You must implement IUnknown as part of every interface. If you are using C++ multiple inheritance to implement multiple interfaces, the various interfaces can share one implementation of IUnknown. If you are using nested classes to implement multiple interfaces, you must implement IUnknown once for each interface you implement.
When To Use

Use IUnknown methods to switch between interfaces on an object, add references, and release objects.

Difference between Mutexes and Critical Sections

They are different synchronization mechanisms.  A mutex has thread affinity, a specific thread owns the mutex.  A critical section is "first-come-first-serve".  A critical section is not waitable like a mutex.  Calling WaitForSingleObject() for a mutex on the thread that owns it immediately succeeds.  If the mutex is owned by another thread, it won't return until the mutex is released.

Difference between Stack vs Heap memory

Stack
Stack memory stores variable types in address' in memory, these variables in programming are called local variables and are often stored for short amounts of time while a function/method block uses them to compute a task.
Once a function/method has completed its cycle the reference to the variable in the stack is removed.

Heap
Heap memory stores all instances or attributes, constructors and methods of a class/object.
Comparison
A Heap reference is also stored in Stack memory until the life cycle of the object has completed. Inside the Heap reference all the the contents of the object are stored whereas with a local variable only the variable contents are stored in the stack.
Example:
Stack
var blue
var red
ref 0x456783 (Heap reference)
var tom
ref 0x498702 (Heap reference)
var diane

Heap (0x456783)
name => Susan
age => 26
city => London
height => 5'7
sex => female

Heap (0x498702)
name => Paul
age => 21
city => Glasgow
height => 6'0
sex => male

Count the number of set bits in a byte/int32

int pop(unsigned x)
{
    x = x - ((x >> 1) & 0x55555555);
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
    x = (x + (x >> 4)) & 0x0F0F0F0F;
    x = x + (x >> 8);
    x = x + (x >> 16);
    return x & 0x0000003F;
}
=======================================================
long count_bits(long n) {     
  unsigned int c; // c accumulates the total bits set in v
  for (c = 0; n; c++) 
    n &= n - 1; // clear the least significant bit set
  return c;
}=======================================================
unsigned int bitCount (unsigned int value) {
    unsigned int count = 0;
    while (value > 0) {           // until all bits are zero
        if ((value & 1) == 1) {   // check lower bit
            count++;
        }
        value /= 2;               // shift bits, removing lower bit
    }
    return count;
}
}
template<class T>
void priority_queue<T>::_dequeue(T& data)
{
 if(root==NULL) return;
 node* pre = root;     //remember the old position of the root node
 while(root->right != NULL) {
  pre = root;
  root = root->right;  //move the root node down the tree to the biggest value
 }//now i've got the biggest element and it's parent
  if(pre != root) {
   //if it doesn't have any childs
   if(root->left == NULL) {
    node* tmp = root;
    data = root->data;
    root = pre;
    root->right = NULL;
    delete tmp;
   } else {
    node* tmp = root;
    data = root->data;
    root = pre;
    root->right = tmp->left;
    delete tmp;
   }
   //if the previous node is the root
  } else {
   data = root->data;
   node* tmp = root;
   root = root->left;
   delete tmp;
  }
}

Arrays

You are given an array with integers between 1 and 1,000,000. One integer is in the array twice. How can you determine which one? Can you think of a way to do it using little extra memory.
Algo:
  • Solution 1:
    1. Have a hash table
    2. Iterate through array and store its elements in hash table
    3. As soon as you find an element which is already in hash table, it is the dup element
      Pros:
      • It runs in O(n) time and with only 1 pass
      Cons:
      • It uses O(n) extra memory
  • Solution2:
    1. Sort the array using merge sort (O(nlogn) time)
    2. Parse again and if you see a element twice you got the dup.
      Pros:
      • it doesn't use extra memory
      Cons:
      • Running time is greater than O(n)

BidVertiser

pocket cents

PocketCents Local Online Advertising