Friday, March 25, 2016

Java Queue Example

What is Queue:

Queue is one of an important data structure which typically (but not necessarily) abide by rule of FIFO (First In First Out) i.e. the element which will be inserted first will be removed from the queue in that insertion order. The time complexity of queue in best case scenario should be O(1) both in case of element insertion or element removal.

Queue in Java:

In Java, Queue (java.util.Queue) is defined as an interface. It means that any java class that can be called as Queue will need to abide by contract defined in java.util.Queue interface by either implementing Queue interface or its sub-interface (BlockingQueue, BlockingDeque, Deque, TransferQueue). If you need skeleton class on top of which you can write up your own queue implementation, then there is AbstractQueue class that will come handy (as name indicates its an abstract class).

Queue Sub-interface:

There are four interface which extends Queue. These are:
    • BlockingQueue - Provide additional feature of thread blocked (go to wait state) while fetching from empty queue and while trying to insert element when the queue is full.
    • BlockingDeque - Provide additional feature of thread blocked (go to wait state) while fetching from empty queue and while trying to insert element when the deque is full.
    • Deque - Deque is double ended queue, which means the insertion and deletion of element is allowed from both end.
    • TransferQueue - The thread inserting element to TransferQueue may wait for any consuming thread to retreive the element. Useful in message passing applications.

Implementation in Java:

There are already ready-to-use Queue (Java Classes) provided in Java library. These are:
    • LinkedList - Similar to Double Linked list i.e. each element has reference to previous and next element. That is why it is preferred over ArrayList when the insertion and deletion operation is more than just reading the list. This is an important interview question on collection framework for fresher as the main difference between ArrayList and LinkedList.
    • PriorityQueue - Inserted elements are ordered as per natural ordering or as per comparator provided. This means in PriorityQueue, the elements is not required to follow FIFO model as there can be comparator which can insert element based on comparator result in between the queue itself.
    • ArrayBlockingQueue - A bounded blocking queue. Consider ArrayBlockingQueue as a fixed size array acting on FIFO model with additional feature of thread gets blocked when queue is full / empty.
    • LinkedBlockingQueue - An optionally bounded blocking queue (Blocking queue feature can be applicable only to bounded collections). Consider LinkedBlockingQueue as Linkedlist following FIFO model with its size can be limited (bounded) or unlimited (Integer.MAX_VALUE). Threads cannot be blocked in unbounded LinkedBlockingQueue during insertion, so blockingqueue feature will not be fully applicable to unbounded LinkedBlockingQueue (only in case of retrieval).
    • PriorityBlockingQueue - An unbounded Priority Queue with feature of Blocking Queue is limited to the retreival of its element (as this queue is unbounded).
    • ConcurrentLinkedQueue - An unbounded LinkedBlockingQueue with thread-safety features. This means multiple threads can insert / delete / retreive elements on ConcurrentLinkedQueue parallely. Since it is unbounded, blocking queue feature is limited to the retreival of element only (i.e. thread attempting to retreive element from empty queue will get blocked only waiting till the insertion of element).
    • ConcurrentLinkedDeque - An unbounded thread safe "double ended queue" based on linked nodes.
    • ArrayDeque - A deque with resizeable array. Since it has array implementation, its performance is better than Stack and Linkedlist. However it is not thread-safe.
    • DelayQueue - An unbounded Blocking queue (no limit on size) to which an element can only be retreived after its delay has expired. 
    • LinkedBlockingDeque - An optionally bounded blocking "double ended queue". If size is specified when creating LinkedBlockingDeque object it will belcome bounded otherwise unbounded. Thread will get blocked in LinkedBlockingDeque while retreiving an empty queue and will get blocked while adding an element to full bounded LinkedBlockingDeque. It is based on linked nodes.
    • LinkedTransferQueue - An unbounded TransferQueue based on linked nodes. The size method is not a constant time operation and may vary.
    • SynchronousQueue - It works by putting the insert operation thread to wait for remove operation by another thread.  In other words, the thread inserting element to Synchronous Queue gets blocked till another thread takes the element out and vice-versa. 
I will provide example of each type of queue in separate article and link it out with above.

Queue Behaviour / Usage :

Queue provides additional way to insert, delete and examine object. For each insert, delete and examine operation, there are corresponding 2 methods, one throws exception and other return special value in case of failure.
Besides this, any Queue will also have all the properties of Collection as Queue interface extends Collection interface

Methods of Queue Interface:

As discussed above, the queue provides 2 methods for each insert, delete and examine operation.
These are:

Insert Operation:

add(e) -> returns true when success and throws exception in case of failure.
offer(e) -> returns true when success and false in case of failure.

Remove Operation:

remove() -> removes the head element. Returns head element when success and throws exception in case of failure.
poll() -> removes the head element. Returns head element when success and returns null in case of failure.

Examine Operation:

element() -> retrieves (but do not remove) the head element. Returns element when true and throws NoSuchElementException in case of failure (i.e. when the queue is empty).
peak() -> retrieves (but do not remove) the head element. Returns element when true and null in case of failure (i.e. when the queue is empty)

Code Example:

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {

 public static void main (String[] args) {
  Queue que = new LinkedList();
  System.out.println("Queue Print:: " + que);
  String head = que.element();
  System.out.println("Head element:: " + head);
  String element1 = que.poll();
  System.out.println("Removed Element:: " + element1);
  System.out.println("Queue Print after poll:: " + que);
  String element2 = que.remove();
  System.out.println("Removed Element:: " + element2);
  System.out.println("Queue Print after remove:: " + que);  

Queue Print:: [first, second, third]
Head element:: first
Removed Element:: first
Queue Print after poll:: [second, third]
Removed Element:: second
Queue Print after remove:: [third]


That's all for example of Queue in Java. I will provide details of each implementing class of Queue Interface in separate post. It is important to mention that all queue methods which throws exceptions needs to be handled wisely.

Thursday, March 24, 2016

JSF Interview Questions And Answers

What is JSF?

JavaServer Faces is a specification for building web based UI (User Interface) for Java Web Applications. It has been made as a standard for Java Community Process. JSF provides widgets like buttons, hyperlinks, checkboxes, etc. in different ways. It has extensible facilities for validating inputs and converting objects to and from strings for display.

What is JavaServer Faces event and listener model?

It provides way to determine the way events emitted by JavaServer Faces UI components are handled.

What is JavaServer Faces conversion model?

JSF conversion model provides way to convert string-based markup generated by JavaServer Faces UI components and server-side Java components (objects) and vice-versa.

What is JavaServer Faces UI class?

JavaServer Faces UI component class defines the properties and behaviour of a JavaServer Faces UI component.

Define JavaServer Faces expression language?

To bind the associated component to a bean property or to bind the associated component's value to a method or an external data source, such as a bean property, simple expression language is being used by a JavaServer Faces UI component tag attributes.

What is JSF life cycle and its phases?

(i) Restore view phase
(ii) Apply request values phase
(iii) Process validations phase
(iv) Update model values phase
(v) Invoke application phase
(vi) Render response phase.

What are tags in JSF?

To enhance the view of the JSP Pages, JSF tags are generally used. Remember JSP pages represent views. Each tag gives rise to an associated component. JSF provides 43 tags in two standard JSF tag libraries: 1. JSF Core Tags Library 2. JSF Html Tags Library.

Tuesday, March 22, 2016

30 REALTIME Puppet Interview Questions and Answers

What is Puppet?
Puppet is the configuration management tool for unix based and windows systems. 
What is puppet manifest?
What is manifest ordering and its importance?
What is puppet module?
Why Puppet matters to Devops? (Advantage of Puppet over other devops tools)
What is puppet catalog?
Can you describe the puppet module layouts / structure?
What are agent nodes?
What is EPP templates?
What is the use of Puppet DB?
What is the use of filebucket in puppet?
How do you perform dry run? (no-op / noop)
What is virtual resource in puppet?
How can you realize a virtual resource?
Is puppet resource idempotent
What is the purpose of Hiera tools?
Which node is called masterless node?
How can you manage nodes using Node Manager?

Monday, March 21, 2016

OpenStack Interview Questions and Answers

Prepare your next OpenStack Interview with these Questions and Answers.

What is OpenStack?

OpenStack is a package of software tools for controlling storage, compute, hypervisors and networking resource in a datacenter. Basically its a software tool for managing IAAS (Infrastructure as a Service) cloud. These days more and more datacenter is migrating to manage its resource on OpenStack.

What are the different OpenStack Services or components?


The GUI component of OpenStack and being used by component administrator.


Component that provides mechanism for user authentication.


Maintains & manages images in different format.


Provides Persistent block storage


 Its provides interface to the networks so that you can access network resources (or you can say "Network as a service").


On user demand, provides instances


It is a storage platform which can be united into application


It provides metering of core components which is used for billing purpose. You have datacenter customers and you want to bill them for its usage.


Offer automated infrastructure deployment


UI component for OpenStack for administrators

Name some hypervisors which OpenStack supports?

(a) Vmware
(b) Xen
(c) KVM
(d) HyperV

What are the modular architectural components of OpenStack?

OpenStack Compute: It maintains huge networks of virtual machines and hypervisors
OpenStack Object Storage: Its a storage system which manages and provides support for object and block storage
Image Service:  This components provides provides discovery and registration for virtual disk images

What are the types of storage provided by OpenStack Compute?

(a) Persistent or Volume Storage
(b) Ephemeral Storage

What is the use of user, role and tenant?

User - member of different projects
tenant - group of users
role - specifies authorization level of the user

What is identity service in openstack?

Openstack identity service is managed by Keystone. Its service is primarily responsibile for:
User Management - Its tracks users alongwith its permission
Service Catalog - As name indicates, it provides catalog for the available service with their api

How to pause and unpause/resume an instance?

To pause an instance - nova pause INSTANCE_NAME
To resume/unpause - nova unpause INSTANCE_NAME

What is the command to create a container?

$ swift post CONTAINER_NAME

What is the command to list and check the status of containers?

To list containers - $ swift list
To check status - $ swift stat CONTAINER_NAME

What are the networking options used in OpenStack?

Flat Network Manager - It doesnt configure ip of the instance and left to system administrator to assign an ip.
Flat DHCP Network Manager -  Its ip is fetched from the subnet.
VLAN Network Manager - Creates a Vlan interface and on each vlan, DHCP server is started to fetch ip for the instances.

What is cells in OpenStack?

Cells functionality enables you to scale up an OpenStack Compute cloud. It supports very large deployment. Here is how its works - the hosts in an OpenStack Compute cloud are partitioned multiple cells/groups, which is configured as a tree.

Cloud Computing Interview Questions

Cloud is an emerging trend these days and that is why these days interviewer is asking more questions on cloud computing. Cloud especially IAAS cloud and virtualization are very close terms and you should also know virtualization interview questions for IAAS. 

Here are some basic questions on cloud computing:

What is cloud computing and why should we go for using cloud computing(advantages)?

Cloud Computing is a model in which configurable and scalable computing resources (storage, network, application etc) are provided as a service to end user over internet with minimal effort.

Basically cloud computing has the following features: 
(a) Utility computing
(b) Programmatic
(c) Grid
(d) On-demand
(e) Scalable
(f) Usage based billing
(g) Virtualization

The advantages / benefits of cloud computing is as under:
(a) Cost effective
(b) Highly scalable
(c) Pay as per use
(d) Only operational expenditure (No setup expenditure)
(e) Data backup 
(f) Quick restore in case of disaster
(g) Software as a service
(h) Minimal management effort
(i) Time Saving

What are different types of cloud?

According to deployment -
  • Public cloud
  • Private cloud
  • Hybrid cloud
  • Community cloud
According to service -
  • IAAS - Infrastructure as a service
  • PAAS - Platform as a service
  • SAAS - Software as a service

What is Public cloud?

A non-proprietory model where computing resources such as storage, network, applications etc are available to general public for use. It generally works on pay per use model. It is self managed solution with shared resources (with other public) with very less control over performance factor.

What is Private cloud?

Private cloud is a model where a set of non-shared computing resource is dedicated to a single organization and is secured enough so that no other client from the same datacenter can access it. Alternatively, it can also be deployed in-house by the organisation. Also, it has more control over the performance of computing resource.

What is Community cloud?

A community cloud is a multiple user cloud where infrastructure is shared amongst multiple participating organisations from a specific community with common concerns (for eg. regulatory compliance etc) . With added layer of privacy, security and policy compliance, Community Cloud has the properties of both Public cloud (Pay as per use) as well as that of Private cloud.

What is Hybrid cloud?

Hybrid cloud is the combination of both Public and Private cloud. Suppose an organisation wants to achieve cost-effectiveness with security then hybrid cloud is the best solution. It can setup private cloud for secured sites and public cloud for sites where security is really not a concern. Since public cloud is relatively cost-effective and is highly scalable as compared to private cloud which is more secure, such a setup achieves both security and cost-effective.

That's all for interview questions on cloud computing.

Vagrant Interview Questions

Originally published by me in another blog at Vagrant Interview Questions and reproduced here for larger audience.
1. What is Vagrant?
2. How Vagrant helps making development environment easy?
3. Can you name some hypervisors on which Vagrant provides wrapper around?
4. Does Vagrant also provides wrapper of dev-ops tools?
5. Which all dev-ops tools does Vagrant provides wrapper over?
6. Does Vagrant supports Docker Containers also?
7. Which server environments Vagrant provides support?
8. What all steps required to configure Vagrant?
9. What are the commands for making Vagrant up and running?
10. What are the benefits of using Vagrant?


11. What is vagrantfile and its use?
12. How Vagrant helps programmers in development?
13. How Vagrant helps operations engineers in devops?
14. How Vagrant benefits software designers?
15. How does Vagrant provider helps you to manage other Virtual Environment
16. How can you share  your environment with others?
17. What is Vagrant Provisioning?
18. What does synced folders do?
19. What is Vagrant Multi-Machine?
20. What is Vagrant box?

If you are planning to learn Vagrant in depth, here are some good books on Vagrant:


You can also purchase it from kindle store directly:


Sunday, March 13, 2016

DevOps Interview Questions

Originally published by me in another blog at DevOps Interview Questions and Answers and reproduced here for larger audience.

  1. What is DevOps and how does it evolve?
  2. What DevOps can do for you?
  3. What is the advantage of DevOps over maven or ant tools?
  4. What are the different DevOps tools present in the market?
  5. Which DevOps tool will you prefer among these?
  6. What is chef, puppet and docker?
  7. What is the advantage of chef over puppet and docker?
  8. What is the advantage of puppet over chef and docker?
  9. What is the advantage of docker?
  10. How DevOps can help in building bridges between Development, QA and Operations teams?
  11. Which scripting language is most significant to learn to become a DevOps engineer?
  12. What all testing required for a successful DevOps project?
  13. What all risks that gets minimized with DevOps?
  14. Suppose there is a bug found in a software that has been already in production and it requires quick fix, will DevOps be helpful in getting it done faster? If yes, how?
  15. How do you access about how 'deployable' a system is?
  16. What is the difference between RAID 1 and RAID 5?
  17. What are the alternatives of init.d in linux?
  18. What roles do QA (Quality Assurance) should play in DevOps according to you?
  19. What long and short term goals a organisation should keep in mind before opting for DevOps?
  20. What testing is necessary to assure a new service is production ready or not?

Wednesday, March 9, 2016

How to create shippable desktop applications in java using Eclipse IDE

Problem Statement:
I want to create a jar which I can run standalone using Eclipse IDE

When the code is ready and all the code is written up and the application is ready to be shipped as standalone application, following are the steps needed:

(a) First recheck whether there is no compilation error.

(b) Confirm all the dependency project and jar is in the build path

(c) Right click on java file which contains main method and click Run As -> Java Application, this will set the launch configuration (see next step)

(d) Now Right click on the project and click "Export...". It will open a Export Dialog Box

(e) On the Export Dialog, select Runnable JAR file inside java tree selection and click Next

(f) Choose from Launch configuration (it will give you the entry point i.e. java file with main method, see step (c) for that)

(g) Choose Export Destination (i.e. jar name and its path where it will be created). Make sure you provide valid path. Use "Browse.." button to choose or select.

(h) select Package required libraries into generated JAR option

(i) click Finish button.

Your jar is ready to be shipped as standalone desktop application.