What is denormalization?

Problem What is denormalization? Explain the pros and cons Solution We have already studied normalization here, but now just study opposite of it. Denormalization is the process of attempting to optimize the performance of a database by adding redundant data or by grouping data. Denormalization is generally used to either : Avoid a certain number of queries Remove some joins Lets take an example. Suppose you are working in a team, which is client facing (say in some investment bank), that serves the trade data to the client. [Read More]

Count the number of calls of put() and get() for a map in Java

Problem Suppose you are using a map in your program, how would you count the number of times the program calls the put() and get() functions? Solution Method 1 - Implement the map interface with static count field One simple solution is to put count variables for get() and put() methods and, whenever they are called, increment the count. We can also achieve this by extending the existing library map and overriding the get() and put() functions. [Read More]

Object reflection in Java

Problem Explain what object reflection is in Java and why it is useful. Solution The name reflection is used to describe code which is able to inspect other code in the same system (or itself). For example, say you have an object of an unknown type in Java, and you would like to call a ‘doSomething’ method on it if one exists. Java’s static typing system isn’t really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called ‘doSomething’ and then call it if you want to. [Read More]

final, finally and finalize in Java

Problem What is the difference between final, finally, and finalize? Solution The keyword “final” means constant. A final variable is a constant variable. A final method cannot be overridden. A final class cannot be extended. The keyword “finally” is for catching exceptions. When all the specified type of exceptions have been caught, the finally block will be executed to handle the rest unspecified exceptions. The keyword “finalize” is for user-defined garbage collection. [Read More]

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]

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]