Finally block and return statement

Problem In Java, does the finally block gets executed if we insert a return statement inside the try block of a try-catch-finally? Solution The answer is yes, both in java and c#. In java, the only time a finally block will not be executed is when you call exit() OR for some reason JVM crashes, before finally is reached. The exit() call will shutdown the JVM, so no subsequent line of code will be run. [Read More]

Private constructor in Java

Problem In terms of inheritance, what is the effect of keeping a constructor private? Solution Private constructors prevent a class from being explicitly instantiated by its callers i.e. it cannot be instantiated. Additionally, because the constructor is private, the class also cannot be inherited. Benefits of this are many : classes containing only static utility methods classes containing only constants type safe enumerations singletons It is used while implementing the Singleton pattern. [Read More]

Smart pointer in C++

Problem Write a smart pointer (smart_ptr) class. Solution As discussed here, smart pointers are C++ objects that simulate simple pointers by implementing operator-> and the unary operator*. In addition to sporting pointer syntax and semantics, smart pointers often perform useful tasks—such as memory management or locking—under the covers, thus freeing the application from carefully managing the lifetime of pointed-to objects. You can read more about it on the provided link. [Read More]

Take a pointer to a Node structure as a parameter and return a complete copy of the passed-in data structure

Problem Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed-in data structure. The Node structure contains two pointers to other Node structures. Solution The algorithm will maintain a mapping from a node address in the original structure to the corresponding node in the new structure. This mapping will allow us to discover previously copied nodes during a traditional depth first traversal of the structure. [Read More]

Searching Questions

Here are some questions on searching Search types Linear Search on Array Binary search on Array - Recursive and iterative Special case of Ranged array By ranged array, I mean that array has some specific size, say N, and numbers in that array can occur in a range of 0 to N-1. I don’t know if some other special name exists for such arrays. But lets go ahead with this name. [Read More]

Find the duplicate element most number of times in array of 0 to n-1

Problem Given an array of n numbers from 0 to n-1, find the element occurring most number of times Solution This problem is similar to following problem : Find the two repeating elements in a given array We can use some of the method used there. Method 1 - Brute force Here we will match the adjacent elements, and increment the count accordingly. Here is the code public int maxDuplicateItemBruteForce(int\[\] A){ int n = A. [Read More]

Destructor in base class needed to be virtual in C++

Problem Why does a destructor in base class need to be declared virtual? Follow up - Is it always required for base class to have virtual destructors? Solution You want them to be virtual so that all subclass destructors are automatically called when the object is destroyed, even if it is destroyed through a pointer to the base class. Calling a method with an object pointer always invokes: [Read More]

“volatile” keyword in C (and Java)

Problem What is the significance of the keyword “volatile” in C? Solution Lets under stand volatile in C Volatile tells the compiler not to optimize anything that has to do with the volatile variable.There is only one reason to use it: When you interface with hardware. Volatile informs the compiler that the value of the variable can change from the outside, without any update done by the code. [Read More]

Deep copy and shallow copy in C++

Problem Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed-in data structure. The Node structure contains two pointers to other Node structures. Solution A shallow copy copies the array and maintains references or copies address to the original objects. A deep copy will copy (clone) the objects too so they bear no relation to the original. [Read More]