Interview Questions

Print Consecutive Occurrences of Each Character in String Java

Posted on

Problem Statement: Write a program to print consecutive occurrences of each character in a given string.

Test case 1:
Input : affffssdaaag
Output: a1f4s2d1a3g1

Test case 2:
Input: affffssdaaa
Output: a1f4s2d1a3

Test case 3:
Input: aaa
Output: a3 Read the rest of this entry »

Count Occurrences of Character in a String Java

Posted on Updated on

Problem Statement: Write a program to count occurrence of each character in a given string.

Test case 1:
Input : aabacghc
Output: a3b1c2g1h1

Test case 2:
Input: aaabbcccc
Output: a3b2c4 Read the rest of this entry »

Lambdas vs Anonymous Inner Classes

Posted on Updated on

Anonymous inner classes are used by java programmer as “ad hoc” functionality i.e where and when they are needed. Since Java 8, we can use lambda expressions instead of anonymous inner classes. It might seems that both inner classes and lambda expressions are similar as they both are used to implement ad hoc functionality. However there are some differences between lambda expression and inner classes which are as follow. Read the rest of this entry »

HashMap vs ConcurrentHashMap

Posted on Updated on

ConcurrentHashMap was introduce with JDK1.5 release to replace legacy class Hashtable. In multithreaded environment ConcurrentHashMap performs better as compared to Hashtable and Synchronized Map as well. All methods of Hashtable are synchronized which makes them quite slow due to contention if a number of thread increases. While ConcurrentHashMap is specially designed for concurrent uses which by default allows 16 threads to simultaneously read and write from map without any external synchronization. Read the rest of this entry »