Telephonic Java Interview Series 43
Telephonic Java Interview Series 43

Java basic

  • List three Collections interfaces and the basic contract of each. List concrete implementations of each, how they differ, and performance characteristics in space and time.

Answer: Java Collections Framework provides a unified architecture for representing and manipulating collections, allowing collections to be manipulated independently of implementation details. Here’s an overview of three primary collection interfaces, their contracts, common implementations, and performance characteristics:

1. List Interface

Basic Contract: The List interface in Java provides a way to store an ordered collection of elements. Elements can be inserted or accessed by their position in the list, using a zero-based index. Lists may contain duplicate elements.

Concrete Implementations:

  • ArrayList: This is a resizable array implementation of the List interface. It provides fast random access and is an excellent choice when there’s a significant amount of read operations.
    • Performance Characteristics:
      • Time: Accessing an element at a specific index is constant time, O(1). Adding an element is O(1) amortized, but removing an element is O(n) because it may require shifting elements.
      • Space: Space is efficiently managed in terms of resizing when the array grows.
  • LinkedList: Implements List as well as the Deque interface. It consists of doubly-linked nodes, which means each node has a link to both the previous and the next node.
    • Performance Characteristics:
      • Time: Adding and removing from the beginning and end are O(1), but accessing an element at a specific position is O(n).
      • Space: Uses more memory than ArrayList due to the overhead of node objects (each node has two additional references).

2. Set Interface

Basic Contract: The Set interface models the mathematical set abstraction and is primarily used to store unique elements. It does not allow duplicate entries and does not maintain any particular order.

Concrete Implementations:

  • HashSet: Backed by a hash table (actually a HashMap instance), it offers excellent performance for adding, removing, and checking if an object exists.
    • Performance Characteristics:
      • Time: Operations such as add, remove, and contains run in constant time O(1) on average.
      • Space: Efficient unless the hash function disperses elements poorly.
  • TreeSet: Implements a set backed by a tree structure (Red-Black tree), thus elements are ordered according to their natural ordering or by a Comparator provided at set creation.
    • Performance Characteristics:
      • Time: Operations like add, remove, and contains are O(log n).
      • Space: Generally requires more memory than HashSet as nodes maintain child/parent links.

3. Queue Interface

Basic Contract: The Queue interface is designed for holding elements prior to processing and typically orders elements in a FIFO (first-in, first-out) manner. Some implementations may provide different orderings.

Concrete Implementations:

  • LinkedList: While primarily a List, LinkedList can also efficiently implement Queue operations.
    • Performance Characteristics:
      • Time: Enqueue (addLast) and dequeue (removeFirst) operations are O(1).
      • Space: As each element requires a node object, the space usage is higher than array-backed implementations.
  • PriorityQueue: A priority queue based on the priority heap concept, this orders elements according to their natural ordering or a Comparator.
    • Performance Characteristics:
      • Time: Insertion and removal operations are O(log n), and retrieval of the head is O(1).
      • Space: Internally represented as a balanced binary heap, so it is more space-efficient than a LinkedList for comparable elements.

Each of these interfaces and implementations offers different performance trade-offs, making them suitable for different types of operations in application development. The choice of a specific collection should be based on the specific requirements regarding order, duplicates, access patterns, and memory usage.

  • Describe the contract of equals and hashCode. Why is it important that if you implement one, that you implement both?
  • What are generics? What is type erasure and what are the consequences? What are variance, covariance and contravariance? If blank stare: why can’t you assign a collection with a generic type binding of a sub type to a reference to the same collection type binding of the super type? How do you specify the type binding of the super type to allow this? How do you specify the type binding to allow type-safe insertion?

Concurrency

  • Explain how notify and notifyAll work, and the difference between the two. Why prefer notifyAll to notify?
  • What is a deadlock and how do you avoid it?
  • What is a race condition and how do you avoid it?
  • What are some of the high-level concurrency classes provided by java.util.concurrent and how do they work?

Database

What are the different statement types and why would you use each?

How do you prevent SQL injection attacks?

What are transactions? What is ACID?

Hibernate

Give an overview of Hibernate and ORM. How do you load objects into the session? What does the session do with the objects while in the session? What is the difference between getting a persistent object from the session and querying for persistent objects?

When is it better to use plain SQL instead of ORM?

Java EE

How do you configure a DataSource in Weblogic to make it available using JNDI?

What are some ways for the client to obtain a reference to the DataSource from the app server? (Spring is not the answer I am looking for)

Spring

Give an overview of how Spring Dependency Injection container works. What is the purpose of DI? How to specify bean definitions? What are the different scopes? When do beans of each scope type get instantiated?

Web Services

What is the difference between SOAP-based web services and REST-based web services?

Describe SOAP and WSDL.

What exactly is REST? What is HATEOAS? What is the purpose of each of the HTTP verbs? What is idempotence? What is content-negotiation?

OOP

What is decoupling? Why are loose-coupling coupled classes desirable? What are some drawbacks?

What is cohesion? Why are highly cohesive classes desirable? What are some drawbacks?

Describe polymorphism. What is the importance of contracts between interfaces and concrete types? Why is polymorphic code desirable? What are some drawbacks?

Leave a Reply

Your email address will not be published. Required fields are marked *