Article 17 - Technical Interview Questions - Design Patterns

Have you flustered in a recent technical interview or are you preparing for an up-coming interview? Many high-tech companies are screening candidates using textbook questions before bringing them in for further interviews. This article contains typical design pattern questions that interviewers will ask. Pay attention.

Singleton Pattern

The singleton pattern permits only one copy of the particular class to be instantiated. Thus, there should only be at most one instantiated object at any given point in time. This fundamental question is not difficult to solve. Simply declare the constructor as private and provide a visible method to instantiate the object. The separate method will then check for the existence of the object before creating one.

Singleton Pattern.

Interviewers may touch up this question by adding concurrency conditions. In Java, one can easily include concurrency by adding the word "synchronized* as part of the method signature. However, this does not work for static methods, which is typically used in the Singleton Pattern. In this case, simply have a private static field that is instantiated statically. Then in the method, you can return this singleton object concurrently. But the problem becomes even harder if the interviewer requires that the object be instantiated lazily; otherwise known as Lazy Instantiation, another design pattern.

Concurrent Singleton Pattern.

As a follow up, you can synchronize on the field declaration of the singleton object in the code block where the check is and then return.

Optimized Concurrent Singleton Pattern with Lazy Instantiation. Note the double check.

Observer Pattern

The Observer Pattern is a typical push and subscribe design. Here, we have a set of subscribers that want to be notified of some events or messages that is published by the publishers. This design is often used for asynchronous designs such as mail systems. It can also be used in user interface designs.

Strategy Pattern

This pattern is very useful for encapsulating detailed and complex implementations while abstracting a common interface. It is widely used.

For example, a sorting class my consist of many types of sorting algorithms, which includes quicksort, bubble sort, and others. We can abstract an interface that a user can call, such as ".sort()", which in the implementation, we can dynamically swap the algorithms that perform the sort. For example, using bucket sort for numeric input, and quicksort for all other sorts.

You should be able to ace the interview.

Comments (27)

Posted by anonymous - bookmaring service at Friday, March 15, 2013 8:39 AM

1LZHYh I loved your blog post.Really looking forward to read more. Great.

Posted by anonymous - generic viagra no prescription at Friday, March 15, 2013 12:49 PM

Very neat blog post.Much thanks again.

Posted by anonymous - generi cialis at Friday, March 15, 2013 2:28 PM

Major thankies for the blog.Much thanks again. Want more.

Posted by anonymous - Buy Levitra online at Friday, March 15, 2013 4:09 PM

This is one awesome blog article.

Posted by anonymous - mac cosmetics at Monday, March 25, 2013 10:21 AM my reference mac cosmetics

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.

Vantasy World Copyright 2011 - 2017. Vantasy World is a project developed by Vantasy Online. Privacy Policy.