Java Hungry

Find the first non repeated character in a string : Technical interview question

In written exams or in technical interview  of Amazon , you can easily find this question . So in order to help you for the preparation of interview , we are answering this question in this post .

First we need to understand the question , we need to write an algorithm for  the first non repeated character in a string , for example :

Read Also :   Count number of words in the String

If the word "stress" is input  then it should print  't'   as output .

If the word "teeter" is input  then it should print  'r'   as output .

Now , we should understand the pseudo algorithm or logic to achieve this task , code is given in the end of this post .


As we know a non repeated character occurs only once in the string , so if we store the number of times
each alphabet appears in the string , it would help us identifying which characters are non repeated characters in the string .So we need to scan the whole string and determine the final counts of each character . Now scan the final values of each character in the string , the first character in the string with final count 1 is the first non repeated character in the string.

Pseudo Algorithm

1.   First create the  character count hash table .

How ConcurrentHashMap Internally Works in Java with Example

How ConcurrentHashMap works or internal implementation of ConcurrentHashMap is one of the most popular java interview questions under the category concurrency. We have already discussed the other popular java interview questions like  internal working of HashSet or How  HashMap works in java .

ConcurrentHashMap utilizes the same principles of HashMap, but is designed primarily for a multi-threaded application and hence it does not require explicit synchronization.   The only thread safe collection objects were Hashtable and synchronized Map prior to JDK 5.

Before learning How ConcurrentHashMap works in Java , we need to look at why ConcurrentHashMap is added to the Java SDK.

Interviewer : Why we need ConcurrentHashMap when we already had Hashtable ?

Hashtable provides concurrent access to the Map.Entries objects by locking the entire map to perform any sort of operation (update,delete,read,create). Suppose we have a web application , the overhead created by Hashtable  (locking the entire map) can be ignored under normal load. But under heavy load , the overhead of locking the entire map may prove fatal and may lead to delay response time and   overtaxing of the server.
This  is where ConcurrentHashMap comes to rescue. According to ConcurrentHashMap Oracle docs,
ConcurrentHashMap class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details. So the main purpose of this class is to provide the same functionality as of Hashtable but with a performance comparable to HashMap.

ConcurrentHashMap achieves this by a simple tweak. So this leads to our main question

Difference between Arraylist and Vector : Core Java Interview Collection Question

Difference between Vector and Arraylist is the most common  Core Java Interview question you will come across in Collection .  This question is mostly used as a start up question by the Interviewers before testing deep  roots of  the Collection  .
Vector , ArrayList classes are implemented using dynamically resizable  array providing fast random access and fast list traversal very much like using an ordinary array . ArrayList  support dynamic arrays that can grow as needed that is ArrayList can be dynamically increased or decreased in size .

Read Also :    Difference between  HashMap and ConcurrentHashMap

Arraylist vs Vector in Java

1.  Synchronization and Thread-Safe

Vector is  synchronized while ArrayList is not synchronized  . Synchronization and thread safe means at a time only one thread can access the code .In Vector class all the methods are synchronized .Thats why the Vector object is already synchronized when it is created .

2.  Performance

Vector is slow as it is thread safe . In comparison ArrayList is fast as it is non synchronized . Thus     in ArrayList two or more threads  can access the code at the same time  , while Vector is limited to one thread at a time.

3. Automatic Increase in Capacity

A Vector defaults to doubling size of its array . While when you insert an element into the ArrayList ,      it increases