Find the nearest numbers that have same number of 1s for an integer

Problem Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation. Example Next higher number for 3 is 5. i.e. (00000011 => 00000101) Likewise next lower number of 5 is 3. Solution Method 1 - Adding or subtracting 1 until we have same number of 1s For the next largest, keep adding 1 to the number until find the number that has same number of 1 bits. [Read More]

Can two threads call a synchronized method and normal method at the same time?

Problem You are given a class with synchronized method A, and a normal method C. If you have two threads in one instance of a program, can they call A at the same time? Can they call A and C at the same time? Solution: Java provides two ways to achieve synchronization: synchronized method and synchronized statement. Synchronized method: Methods of a class which need to be synchronized are declared with “synchronized” keyword. [Read More]

Scheduling method calls in sequence

Problem Suppose we have the following code: class Foo { public: A(.....); // If A is called, a new thread will be created // and the corresponding function will be executed. B(.....); // same as above C(.....); // same as above } Foo f; f.A(.....); f.B(.....); f.C(.....); i) Can you design a mechanism to make sure that B is executed after A, and C is executed after B? ii) Suppose we have the following code to use class Foo We do not know how the threads will be scheduled in the OS: [Read More]

Thread safe and exception safe singleton design pattern

Problem Implement a singleton design pattern as a template such that, for any given class Foo, you can call Singleton::instance() and get a pointer to an instance of a singleton of type Foo Assume the existence of a class Lock which has acquire() and release() methods How could you make your implementation thread safe and exception safe? Solution Here is the code in cpp: using namespace std; // Place holder for thread synchronization lock class Lock { public: Lock() { // placeholder code to create the lock } ~Lock() { // placeholder code to deallocate the lock } void AcquireLock() { // placeholder to acquire the lock } void ReleaseLock() { // placeholder to release the lock } }; // Singleton class with a method that creates a new instance // of the \* class of the type of the passed in template // if it does not already exist. [Read More]

How to measure the time spent in a context switch

Problem How can you measure the time spent in a context switch? Solution This is a tricky question, but let’s start with a possible solution. A context switch is the time spent switching between two processes (e.g., bringing a waiting process into execution and sending an executing process into waiting/terminated state). i.e. it is the computing process of storing and restoring the state (context) of a CPU so that execution can be resumed from the same point at a later time. [Read More]

Common routing protocol

Problem Explain any common routing protocol in detail. For example: BGP, OSPF, RIP. Solution BGP: Border Gateway Protocol BGP is the core routing protocol of the Internet . When a BGP router first comes up on the Internet, either for the first time or after being turned off, it establishes connections with the other BGP routers with which it directly communicates. The first thing it does is download the entire routing table of each neighboring router. [Read More]

Compare and contrast IPv4 and IPv6

Problem Compare and contrast the IPv4 and IPv6 protocols. Solution IPv4 and IPv6 are the internet protocols applied at the network layer. IPv4 is the most widely used protocol right now and IPv6 is the next generation protocol for internet. Table Cell Table Cell IPv4 is the fourth version of Internet protocol which uses 32 bit addressing IPv6 is a next generation internet protocol which uses 128 bits addressing. [Read More]

What is a network/subnet mask

Problem What is a network / subnet mask? Explain how host A sends a message / packet to host B when: (a) both are on same network and (b) both are on different networks. Explain which layer makes the routing decision and how. Solution A mask is a bit pattern used to identify the network/subnet address. The IP address consists of two components: the network address and the host address. [Read More]

What happens after typing a URL into a browser

Problem Explain what happens, step by step, after you type a URL into a browser. Use as much detail as possible. Solution Take the URL of my website as example: k2code.blogspot.com. There’s no right, or even complete, answer for this question This question allows you to go into arbitrary amounts of detail depending on what you’re comfortable with. Here’s a start though: Lets assume a simple http request. [Read More]

Allocate a two dimensional array using one call of malloc in C

Problem Write a function called my2DAlloc which allocates a two dimensional array. Minimize the number of calls to malloc and make sure that the memory is accessible by the notation arr[i][j]. Solution We will use one call to malloc. Allocate one block of memory to hold the row vector and the array data. The row vector will reside in rows * sizeof(int*) bytes. The integers in the array will take up another rows * cols * sizeof(int) bytes. [Read More]