Blog Interview Experiences Synechron

Interview at Synechron

Question: You are given a String that contains alphanumeric characters (letters and digits) as well as special characters like !, @, #, and whitespaces. The task is to count the occurrences of only the special characters present in the String, ignoring the letters, digits.


package interview.synechron;

import java.util.Map;
import java.util.function.Predicate;

//a z, A -> Z, 0 -9

public class Main {
    public static void main(String[] args) {
        String str ="Hj@34ch @ hj # jj";
        Map<Character, Long> map = str.chars().mapToObj(c -> (char)c)
                .filter(c -> !Character.isAlphabetic(c)  && !Character.isWhitespace(c))
                        .collect(Collectors.groupingBy(c ->c, Collectors.counting()));

        map.entrySet().stream().forEach(e -> System.out.println("Character:"+ e.getKey() +", Count:"+ e.getValue()));


Question: What are the multiple ways in which we can break a singleton design pattern? How do I make sure that with the help of reflection,How do you handle deserialization issue? Can multi-threading break that design pattern? 

Question: How is your ConcurrentHashMap different from a normal HashMap?

ConcurrentHashMap and HashMap are both key-value storage data structures provided by Java, but they have some important differences:

  1. Thread-Safety: ConcurrentHashMap is thread-safe, which means it can be safely used by multiple threads concurrently without any external synchronization. On the other hand, HashMap is not thread-safe, and concurrent modifications by multiple threads can lead to data inconsistency.
  2. Performance: ConcurrentHashMap provides better performance than a synchronized HashMap because it allows concurrent reads and thread-safe writes without blocking the entire map. This is achieved by dividing the map into segments and locking only a particular segment during write operations.
  3. Null Keys/Values: HashMap allows one null key and any number of null values, while ConcurrentHashMap does not allow null keys or values. This is because nulls are not handled in a thread-safe manner and can lead to issues when used concurrently.
  4. Iterators: The iterator of HashMap is fail-fast, which means it will throw a ConcurrentModificationException if the map is modified while iterating over it. On the other hand, the iterator of ConcurrentHashMap is weakly consistent, which means it won’t throw any exceptions if the map is modified during iteration, but it might not reflect the latest changes.

Here’s a simple comparison table:

Allows null keys/valuesYesNo
Fail-Fast IteratorYesNo
Performance (concurrent use)LowHigh

In summary, if you need a map in a multi-threaded environment, ConcurrentHashMap would be a better choice due to its thread-safety and higher performance. However, if you’re working in a single-threaded environment or don’t require thread-safety, HashMap would suffice.

Question: HashMap, can two threads simultaneously write in two different buckets for a normal HashMap?

Without using any external synchronization mechanisms, two threads should not attempt to write simultaneously to different buckets of a regular (non-concurrent) HashMap in Java.

Even though the writes are happening in different buckets, the underlying HashMap data structure is not thread-safe for concurrent modifications. The internal state of the HashMap, such as the size field, modCount, and potentially other internal data structures, could become inconsistent if multiple threads modify the HashMap concurrently without proper synchronization.

While the writes may be happening in different buckets, there are still shared resources within the HashMap that need to be modified in a thread-safe manner during write operations. Without synchronization, there is a risk of race conditions, where one thread’s modifications could corrupt the state that another thread is working with.

So, to summarize, without any external synchronization mechanisms like locks or other synchronizers, it is not safe for two threads to write simultaneously to different buckets of a regular (non-concurrent) HashMap in Java. Doing so could lead to unpredictable behavior, data corruption, or other concurrency issues.

If you need to perform concurrent write operations on a hash map data structure from multiple threads, it is recommended to use the thread-safe ConcurrentHashMap instead of the regular HashMap. ConcurrentHashMap is designed to handle concurrent reads and writes safely by employing techniques like lock striping and bucket-level locking.

Question:How is locking of a normal HashMap different from a concurrent HashMap?  

The locking mechanism used in a normal HashMap and a ConcurrentHashMap in Java is fundamentally different, which affects how they handle concurrent access from multiple threads.

Normal HashMap:

  • In a normal HashMap, the locking is done at the object level.
  • Whenever a thread needs to perform any operation (read or write) on the HashMap, it acquires a single lock on the entire HashMap object.
  • This means that only one thread can access the HashMap at a time, effectively serializing all operations.
  • If a thread is performing a read or write operation, all other threads attempting to access the HashMap (even for read operations) will be blocked until the lock is released.
  • This approach ensures thread-safety but can lead to contention and reduced concurrency, especially in read-heavy workloads.


  • In a ConcurrentHashMap, the locking is done at a finer-grained level, using a technique called lock striping.
  • The ConcurrentHashMap is internally divided into multiple segments or buckets, each with its own lock.
  • When a thread needs to perform an operation on a specific key-value pair, it acquires the lock only for the specific segment/bucket that the key belongs to, rather than locking the entire map.
  • This allows multiple threads to operate concurrently on different segments/buckets of the ConcurrentHashMap, improving concurrency and throughput.
  • Read operations in ConcurrentHashMap generally do not acquire any locks, as they are designed to be lock-free for better performance in read-heavy workloads.
  • Write operations, however, need to acquire the lock for the specific segment/bucket where the key-value pair resides.

The key difference is that in a normal HashMap, all operations are serialized due to the single object-level lock, while in a ConcurrentHashMap, operations can happen concurrently on different segments/buckets, thanks to the finer-grained locking mechanism.

This difference in locking strategy makes ConcurrentHashMap more suitable for scenarios where high concurrency and better performance are required, especially in read-heavy workloads. However, the trade-off is increased complexity and potentially higher memory overhead due to the additional locking and synchronization mechanisms used in ConcurrentHashMap.

Question: what is the time complexity of an ArrayList when it comes to a searching operation? 



About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Tech Toolkit

Base64 Decode

Base64 encoding is a technique used to encode binary data into ASCII characters, making it easier to transmit data over