Q: Find the greatest common divisor of 2 numbers a and b and also find 2 numbers x and y such that ax + by = GCD(a,b)
Sol:
Q: You are given two singly linked lists, such that they start from two different nodes (head) and then, a few nodes down the list, they meet at a common node and then share all the nodes henceforth until the tail. The task is to find the first common node.
Sol:
Buffer Overruns is one of top sources of security issues today. These are typically caused by trusting input data to a function that is external and is unchecked. Most of the times, this is unintentionally invoked by bad sloppy code. However, when done intentionally by a hacker, this can cause havoc. Some of the most common, buffer overrun prone functions include strcpy, memcpy, strcat etc... In unintentional buffer overruns, this mostly results in writing to memory not owned by your processes address space.
Reading the declaration right to left is a easy way to remember what they mean.
Constructors do not have return type and so they cannot return error codes. How are errors or exceptions handled in constructors? What if the calls that you make in the constructor can actually throw exceptions? How do you let the caller know something bad happened in a constructor?
There are a few ways to do robust error/exception handling in constructors
Swapping variables is a very common operation used it tons of algorithms like sorting etc. The most common and obvious technique is using of a temporary variable to swap values between two variables
void swap(int &i, int &j)
{
int temp = i;
i = j;
j = temp;
}
Instead of writing a separate function for each data type, you could write a MACRO or templatize the function.
Swapping without using a temporary variable is an age old trick and there are a few ways to do this. You could use of the basic arithmetic operations like +,-,/,*
Singleton Pattern: Part 3 Double Checked Locking
A normal search across a BST (Binary Search Tree) would look like this
bool BinaryTree::Search (int data )
{
Node *current = this->root;
while ( current != NULL )
{
if (current->data < data)
{
current = current->left;
}
else if (current->data > data)
{
current = current->right;
}
else if ( current->data == data )
{
return true;
}
}
return false;
}
Binary Tree Traversal: Breadth First aka Width First aka Level Order
This is the lesser know of the different kinds of binary tree traversals. Most beginner books and articles only cover the depth first searches. Breadth first traversals are an extremely important tool when working with Binary Trees. The idea is pretty nifty. Basically you work with a Queue, and push the root node into the Queue. Then do the following until you have visited all nodes in the tree.