Cracking Core Java Interviews - v3.5
Cracking Core Java Interviews - v3.5
Cracking Core Java Interviews - v3.5
JAVA
INTERVIEWS
Preface
This work is my sincere effort to consolidate solutions to some basic set of problems faced by my fellow mates
in their day to day work. This work can be used by candidates preparing to brush up their skills for Job change.
This Book Is
• Collection of excerpts discussing the common problems faced by experienced Java developers in their
day to day work. The intent is not to provide with the concrete solution to a given problem, but to show the
approach to get the problem solved. Possibly there could be more efficient ways to solve the given problem
compared to what has mentioned in this book. The approach shown here is limited to the knowledge of the
author.
• Collection of questions covering Core Java, Object Oriented Design, Concurrency, Algorithms & Data
Structures, Spring, Hibernate, REST and few puzzles.
Munish Chandel
[email protected]
https://www.linkedin.com/in/munishchandel
August 2018
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 4
Cracking Spring
Microservices
Interviews for Java
Developers
Buy Now
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 5
Contents
Cracking Java Interviews 2
Preface3
Q 1. What are good software practices for developing Scalable, Testable and Maintainable software? 15
Q 2. What is growth roadmap for a Java developer? 16
Q 3. What are essential skills for Investment Banking domain? 17
Q 4. What are trending skills in Information & Technology? 17
Q 5. What are good books for Java developers? 17
Q 6. Why should one choose Java for Software Development? What are pros and cons of Java? 18
Q 7. What is difference between 32 bit and 64 bit versions of Java? 18
Q 8. What is difference between JDK, JRE and JVM? 19
Q 9. What are four basic principles of Object Oriented Programming? 19
Q 10. What is Aggregation, how is it different from Composition? 21
Q 11. Why do I need to put effort in understanding business requirements? 22
Q 12. What is a Logarithm? Why is it so relevant in algorithms & datastructures? 24
Q 13. What is 2's complement notation system? 25
Q 14. What do you understand by Big O notation, why is it important in algorithms? 26
Q 15. How would you determine Time Complexity of a given algorithm, are there any general guidelines? 27
Q 16. What is a sorting algorithm? List down sorting algorithms by their time & memory complexity in Big O
notation? When do we call a sorting algorithm 'Stable'? 28
Q 17. Why Prime numbers are so important for certain algorithms like RSA & hashcode? 34
Q 18. What is left shift <<, right shift >> and Unsigned right shift >>> operator in Java? How are these useful?34
Q 19. Why do interviewer ask questions about Garbage Collection? 35
Q 20. How heap memory is divided in Java. How does Garbage Collector cleans up the unused Objects? Why
shouldn't we use System.gc() command in production code? 36
Q 21. What is difference between Stack and Heap area of JVM Memory? What is stored inside a stack and what
goes into heap? 40
Q 22. What is a Binary Tree? What are its usecases? 41
Q 23. Discuss implementation and uses of TreeSet Collection? 41
Q 24. How does Session handling works in Servlet environment? 42
Q 25. Explain Servlet Life Cycle in a Servlet Container? 43
Q 26. How can one handle relative context path while coding the web applications? For example, your web
application may be deployed at a different context path in Tomcat, how will you make sure static/dynamic
resources works well at custom context path? 44
Q 27. How will you write a simple Recursive Program? 45
Q 28. How many elements a complete binary tree could hold for a depth of 10? 45
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 6
Q 29. How will you swap two numbers without any temporary variable? 46
Q 30. Explain working of a hashing data structure, for example HashMap in Java. 47
Q 31. Discuss internal's of a concurrent hashmap provided by Java Collections Framework. 48
Q 32. Discuss Visitor, Template, Decorator, Strategy, Observer and Facade Design Patterns? 50
Q 33. What is a strong, soft, weak and phantom reference in Java? 52
Q 34. What is transaction Isolation level? 54
Q 35. What is difference between Primary key and Unique Key? 55
Q 36. Why do we need indexing on database table columns? 55
Q 37. How will you list all customers from customer table who have no Order(s) yet? 56
Q 38. How would you fetch Employee with nth highest Age from Employee Table using SQL? 56
Q 39. What is difference between Drop, Truncate and Delete commands in SQL? 56
Q 40. What is Inner Join, Left Outer Join and Right Outer Join? 57
Q 41. What are clustered and non-clustered indexes in database? 58
Q 42. How would you handle lazily loaded entities in web application development using hibernate? 58
Q 43. What are OneToOne, OneToMany and ManyToMany relationship mappings in database design? 59
Q 44. How would you implement ManyToMany mappings with the self entity in JPA? 60
Q 45. What are Inheritance strategies in JPA? 61
Q 46. How will you handle Concurrent updates to an database entity in JPA i.e. when two users try to update the
same database entity in parallel? 61
Q 47. How to efficiently generate ID's for an Entity in Hibernate/JPA? 62
Q 48. How does a typical Hibernate Transaction look like? 62
Q 49. How will you handle batch insert in hibernate for optimal usage of memory, network and CPU? 63
Q 50. How will you operate on records of a large database table with million of entries in it using Hibernate? 64
Q 51. Do you think Hibernate's SessionFactory and Session objects are thread safe? 65
Q 52. What is difference between Hibernate's first and second level cache? 65
Q 53. What is difference between session.get() and session.load() in hibernate? 65
Q 54. What is usecase for GET, PUT, POST and DELETE method in REST API? 66
Q 55. What are different types of Http Status Codes? 66
Q 56. What is difference between HTTP Redirect and Forward? 67
Q 57. What are the best practices for handling TimeZone in database transactions? 67
Q 58. How will you check the owner information of a given domain name in web? 68
Q 59. What happens when you type www.google.com in your browser's address bar from an Indian Location?69
Q 60. Why do we need Spring Framework? 71
Q 61. What is Inversion of Control (or Dependency Injection)? 71
Q 62. What is Bean Factory in Spring? 71
Q 63. What is Application Context? 71
Q 64. What are different types of Dependency Injection that spring support? or in other words what are the
ways to initialize beans in Spring? 72
Q 65. What are different Bean Scope in Spring? 72
Q 66. What are some important Spring Modules? 72
Q 67. How will you load hierarchy of property files in Spring Context? 72
Q 68. How to handle Bean Post Initialization and Pre Destroy Tasks in Spring Framework? For example resource
loading after bean construction and resource cleanup before shutdown of spring context? 73
Q 69. What is syntax of Cron Expression? 74
Q 70. How will you embed a PNG/GIF image inside a CSS file? 74
Q 71. Explain Java 8 Stream API? 75
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 7
Q 101. Is it possible to write a method in Java which swaps two int/Integer? 102
Q 102. What all collections require hashcode() method? 103
Q 103. What is problem with mutable static variables in Java class? 103
Q 104. Provide a diagram for collections framework. 104
Q 105. What is Immutable Class. Why would you choose it? How would you make a class immutable? 105
Q 106. Discuss Exception class hierarchy in Java. When should we extend our custom exception from
RuntimeException or Exception? 106
Q 107. How does an ArrayList expands itself when its maximum capacity is reached? 107
Q 108. How does HashMap expands itself when threshold is reached? 107
Q 109. What is StringPool In Java? 107
Q 110. What is instance level locking and class level locking? 108
Q 111. Explain threading jargons? 109
Q 112. What is float-int implicit conversion while doing calculation on mixed data type in Java? 110
Q 113. Discuss Comparable and Comparator? Which one should be used in a given scenario? 110
Q 114. How would you sort a collection of data based on two properties of an entity in Java, analogical to SQL's
Order by firstField, SecondField desc? 111
Q 115. How would you convert time from One Time Zone to another in Java? 112
Q 116. Will WeakHashMap's entry be collected if the value contains the only strong reference to the key? 113
Q 117. Why HashMap's initial capacity must be power of two? 113
Q 118. Can we traverse the list and remove its elements in the same iteration loop? 113
Q 119. Do I need to override object's equals() and hashcode() method for its use in a TreeMap? 114
Q 120. Implement a thread-safe BlockingQueue using intrinsic locking mechanism. 114
Q 121. Is there a way to acquire a single lock over ConcurrentHashMap instance? 115
Q 122. How will you implement a Blocking Queue using Lock and Condition Interface provided in JDK? 115
Q 123. How would you cancel a method execution after time-out expires using Java Future? 116
Q 124. Java already had Future interface, then why did they provide Completable Future class in Java 8? 117
Q 125. What is difference between intrinsic synchronization and explicit locking using Lock? 118
Q 126. What are Stamped Locks? How they are useful in optimistic scenario where thread contention is rare?
119
Q 127. How will you find out first non-repeating character from a string? For example, String input =
"aaabbbeggh", answer should be 'e' 121
Q 128. What is difference between Callable and Runnable Interface? 122
Q 129. What will happen when an exception occurs from within a synchronized code block? Will lock be
retained or released? 122
Q 130. What is difference between sleep(), yield() and wait() method? 123
Q 131. What is difference between StringBuilder and StringBuffer? 123
Q 132. How does Generics work in Java? 124
Q 133. What are Upper and Lower bounds in Generics? Where to choose one? 125
Q 134. Discuss memory visibility of final fields in multi-threading scenario. 126
Q 135. Where would you use LinkedHashSet provided by Java Collections? 128
Q 136. What do you think is the reason for String Class to be Immutable? 128
Q 137. How is String concatenation implemented in Java using + operator?
for example, String name = "hello" + "world" 128
Q 138. Which data type would you choose for storing sensitive information, like passwords, and Why? 129
Q 139. What is difference between using Serializable & Externalizable Interfaces in Java? 129
Q 140. How would you design Money Class in Java? 129
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 9
Q 145. What is Concurrency? How will you implement Concurrency in your Java Programs? 137
Q 146. There are two Threads A and B operating on a shared resource R, A needs to inform B that some
important changes has happened in R. What technique would you use in Java to achieve this? 138
Q 147. What are different states of a Thread? What does those states tells us? 139
Q 148. Question: What do you understand by Java Memory Model? What is double-checked locking? What is
different about final variables in new JMM? 140
Q 149. Is i++ thread-safe (increment operation on primitive types)? 144
Q 150. What happens when wait() & notify() method are called? 144
Q 151. Discuss ThreadPoolExecutor? What different Task Queuing Strategies are possible? How will you
gracefully handle rejection for Tasks? 144
Q 152. How will you write a custom ThreadPoolExecutor that can be paused and resumed on demand? You can
extend the existing ThreadPoolExecutor to add this new behavior. 146
Q 153. How will you write your own custom thread pool executor from scratch? 147
Q 154. What is difference between ExecutorService's submit() and execute() method? 148
Q 155. Discuss about volatile keyword and Java Memory Model? 149
Q 156. What is a CAS? How does it help writing non-blocking scalable applications? Tell something about
Atomic Package provided by Java 1.6 150
Q 157. There is a object state which is represented by two variables. How would you write a high throughput
non-blocking algorithm to update the state from multiple threads? 151
Q 158. How would you implement AtomicFloat /AtomicDouble using CAS? 152
Q 159. How LongAdder and LongAccumulator are different from AtomicLong & AtomicInteger? 154
Q 160. Can we implement check & update method (similar to compare and swap) using volatile alone? 154
Q 161. What is difference between Fork/Join framework and ExecutorService? 155
Q 162. How does ForkJoinPool helps in writing concurrent applications? Please provide few examples for
RecursiveTask and RecursiveAction. 155
Q 163. How will you track the largest value monitored by different threads in an non-blocking fashion (using
atomic operations)? 156
Q 164. How will you calculate Fibonacci Sequence on a multi-core processor? 159
Q 165. How will you increment each element of an Integer array, utilizing all the cores of processor? 160
Q 166. You are writing a multi-threaded software piece for NSE for maintaining the volume of Trades made by
its individual brokers (icici direct, reliance ). It's highly concurrent scenario and we can not use lock based thread
safety due to high demand of throughput. How would handle such scenario? 161
Q 167. Calculate the time spread for 10 threads - Suppose T1 started earliest and T5 finished last, then the
difference between T5 and T1 will give time spread. 162
Q 168. What are fail-fast Iterator? what is fail safe? 164
Q 169. There is a stream of words which contains Anagrams. How would you print anagrams in a single bucket
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 10
Q 179. G iven a collection of 1 million integers ranging from 1 to 9, how would you sort them in O(n) time? 175
Q 180. Given 1 million trades objects, you need to write a method that searches if the specified trade is
contained in the collection or not. Which collection would you choose for storing these 1 million trades and
why?176
Q 181. I have an Integer array where every number appears even number of time except one. Find that number.
176
Q 182. how would you check if a number is even or odd using bit wise operator in Java? 177
Q 183. How would you check if the given number is power of 2? 177
Q 184. What is a PriorityQueue? How is it implemented in Java? What are its uses? 178
Q 185. What is difference between Collections.sort() and Arrays.sort()? Which one is better in terms of time
efficiency?179
Q 186. There are 1 billion cell-phone numbers each having 10 digits, all of them stored randomly in a file. How
would you check if there exists any duplicate? Only 10 MB RAM is available to the system. 179
Q 187. What is a Binary Search Tree? Does Java provide implementation for BST? How do you do in-order,
pre-order and post-order Traversal of its elements? 180
Q 188. What is technique to sort data that is too large to bring into memory? 181
Q 189. Check if a binary tree is a Binary Search Tree or not? 181
Q 190. How would you convert a sorted integer array to height balanced Binary Search Tree?
Input: Array {1, 2, 3}
Output: A Balanced BST
2
/ \
1 3 182
Q 191. How would you calculate depth of a binary tree? 183
Q 192. Calculate factorial using recursive method. 183
Q 193. You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts
and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt
together, you can see which is bigger. But it is not possible to directly compare two nuts or two bolts. Given an
efficient method for solving the problem. 184
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 11
Q 194. Your are give a file with 1 million numbers in it. How would you find the 20 biggest numbers out of this
file?184
Q 195. Reverse the bits of a number and check if the number is palindrome or not? 185
Q 196. How would you mirror a Binary Tree? 185
Q 197. How to calculate exponentiation of a number using squaring for performance reason? 186
Q 198. How will you implement a Queue from scratch in Java? 187
Q 199. How will you Implement a Stack using the Queue? 188
Q 200. How will you test if a given sentence is a Pangram or not? 189
Q 201. How would you implement a simple Math.random() method for a given range say (1-16)? 190
Q 202. How an elevator decides priority of a given request. Suppose you are in an elevator at 5th floor and one
person presses 7th floor and then 2nd presses 8th floor. which data structure will be helpful to prioritize the
requests? 190
Q 203. How would you multiply a number with 7 using bitwise hacks? 191
Q 204. What is the best way to search an element from a sorted Integer Array? What would be it's time
complexity?191
Q 205. How would you reverse a Singly linked List? 192
Q 206. How would you count word occurrence in a very large file? How to keep track of top 10 occurring
words?193
Q 207. What is difference between synchronized HashMap and a Hashtable? 197
Q 208. What is difference between Iterator and ListIterator? 197
Q 209. What do you understand by Token Bucket Algorithm. What is its use? 198
Q 210. How will you implement fibonacci series using Iterative & Recursive approach in Java 8? 200
Q 211. How will you write a multi-threaded HttpDownloader program using Java 8? 203
Q 212. How will you find first non-repeatable character from a String using Java 8? 204
Q 213. How will you find Word Frequency in sorted order for a collection of words? 204
Q 214. How will you calculate MD5 hash of a given String in Java? 206
Q 215. There is a set of integer values representing the time consumed by a job execution in seconds, how will
you find average execution time ignoring the extreme run times (0 seconds or a value much above the average
execution time)? 206
Q 216. There are millions of telephone numbers in a country, each state of the country has specific phone
number range assigned to it. How will you determine which state a number belongs to? 206
Q 217. There is a number series in which subsequent number is either +1 or -1 of previous number. How will
you determine if the range of supplied numbers are contained in this series in minimum time? 208
Object Oriented Design 209
Q 218. What are the key principles when designing a software for performance efficiency? 209
Q 219. How would you describe Producer Consumer problem in Java? What is its significance? 209
Q 220. How would you implement a Caching for HttpDownloader Task using Decorator Design Pattern? 213
Q 221. Write Object Oriented design for library management system. 214
Q 222. Design ATM machine. 216
Q 223. Design a web crawler that will crawl for links(urls). 217
Q 224. Design Phone Book for a mobile using TRIE (also known as prefix tree). 221
Q 225. How would you resolve task's inter dependency, just as in maven/ant.
Let's consider the following task dependencies.
Here first row states that task 3 is dependent on task 1 and task 5, and so on. If the two consecutive tasks have no
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 12
Q. What causes a typical developer to switch his/her job so frequent, Is that bad, why that is not the
case in West?
A thought to switch Job comes to one's mind when one finds blockage in growth opportunities in their current
organization. I could list few reasons for the same -
1. Salary disparity and scope for higher salaries in next Job is the major reason for job switch. Most service
based companies tries to maximize their profit offering lower salaries to its employees (that's beneficial for
freshers who do not have any experience), but as people acquire more skills they switch for bigger roles in
new Job. Demand and supply typically governs the salaries in India.
2. The quality of work is another reason for switch. Work quality directly relates to stress (more manual work
more stress)
3. Shift Timings and location preference also causes people to switch their jobs
To some extent this switch is fair because we can't expect someone to work for a company few thousand
dollars a year for his lifetime (As he acquires skills in parallel to take up more responsibilities). As the Industry
will mature, job shift will reduce. The IT companies in west are mature, salaries are already saturated, people
don't take much work stress, So western employees do not find many reasons for their Job switch.
Q. What is typical psychology of average Indian developer? What kind of chaos pollute his mind?
Most Indian opt for IT, not by choice but for money, because of large unemployment in India. Moreover earning
money in IT industry is easy and effortless compared to other parallel opportunities. Many people wants to
Chapter - Introduction Cracking Java Interviews (Java 8, Hibernate & Spring) 14
take IT as the jumping ground for their higher studies (MBA, MS, etc). An average fresher is polluted with the
thoughts about his career growth, and is unsure about his key interests in IT field, trying various alternates in
first few years.
Q. Why do interviewers ask rocket science questions in interviews even if the job profile does not
require any such skills?
Hiring in IT industry is not regulated (it should neither be) by any means, it is solely up to the interviewer to
choose the topic for discussion in the interview. In today's intellectual world, people like intellectual war, and
interview is a good place for that. I do not find any harm by such interview process unless interviewer hides
the real picture of work that one needs to perform after joining the new role. For sure there is one plus point to
such interview process that it will definitely tend to raise our skill set.
Q. Why people take so many offers at the time of job change, doesn't it add to chaos?
The main reason for doing so is the disparity between work and salary across the companies. People feel
insecure at financial level and try their best to grab the most paying job opportunity, and that's fair from
employee perspective. On the other hand, companies tend to maximize their profit by limiting the salary offer
as per individual's previous company's salary. So it is a game, where both the employer and the employee are
fighting to maximize their own profit. Ultimately, the demand and supply equation balances the fight between
employer and the employee. Saturation of salaries and work quality in coming years might improve this
situation.
Chapter 1
Core Concepts, Spring & Hibernate
Q 1. What are good software practices for developing Scalable, Testable and Main-
tainable software?
1. Proper understand of the domain knowledge is first step towards software development.
2. Microservices Architecture is the current industry trend for developing scalable distributed systems.
Spring Boot and Spring Cloud provide the necessary boiler plate code for developing cloud native
applications in Java.
3. Make system resilent to failures by using circuit breaker & bulk head design patterns. During peak load, its
better to turn off a single failed component of a big system rather than choking the entire system.
4. Make distributed systems more reliable and eventually consistent by using messaging queues
(Advanced Message Queuing Protocol - RabbitMQ, Kafka, JMS, etc). This also allows for asynchronous
communication between services. Asynchronous communication brings many benefits (loosely coupled,
increased throughput, fault-tolerant reliable, etc.) to the distributed computing.
5. Follow good software development practices like Agile with Test Driven Development. Agile development
is all about incorporating changes in the software without much pain. TDD helps achieving agility in your
software. A good test coverage (End to End and Unit Tests) keeps a developer away from last minute
stress during production deployment.
6. Automate all non-productive mundane tasks related to deployment, using Gradle, Jenkins, Salt Stack etc.
7. Keep refactoring your code base time to time, don't leave any duplicate code inside code base. Follow DRY
(don't repeat yourself) strictly. Every object must have a single authoritative representation in the system.
Software development is like the art of gardening where refactoring takes it to a next level.
8. Add an automated test case for every new bug found. Appropriate test assertions are equally important
otherwise it will just reflect on the code coverage without much help.
9. Use Contract Tests to make sure that your REST consumers are not affected by your changes.
10. Use Profiling Tools to identify bottlenecks of your application. One can use jvisualVM tool bundled in JDK
to know the JVM profile of an application, though there are some commercially available easy to use tools
available in market, e.g. JProfiler
11. Use tools to find the duplicates and then refactor to reuse the existing code with better design. IntelliJ is
one of the good tools that will take care of boilerplate stuff (but soon you will become it's luxury addict)
12. Work in small steps with frequent feedback loop to avoid the last minute surprises (Agile). Github 1is well
known for its aggressive engineering practices, deploying code into production on an average 60 times
a day. Amazon is on record2 as making changes to production every 11.6 seconds on average in May of
2011.
13. Continuous Integration environment is must for rapid bug free, coordinated development. Tools like
TeamCity, Hudson, Jenkins, etc can be leveraged for Continuous Integration.
14. Software development without Art, Fun and Creativity is boring and will bring bugs and depression, so be
aware of this warning sign. Don't leave learning, be a student for lifetime!!!
1 https://githubengineering.com/move-fast/
2 https://www.thoughtworks.com/insights/blog/case-continuous-delivery
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 16
Its not possible & necessary to have hold over all the above technologies, so you must decide the ones for you
as per your interest and capabilities.
Q 6. Why should one choose Java for Software Development? What are pros and
cons of Java?
Java Pros
1. Java is free, download it and start creating your own applications. Linux, MySQL and Java have been a
favorite choice for startups as well as big MNCs. Now a new major release will arrive every 6 months.
2. Spring (specifically Spring Boot and Spring Cloud) makes development a fun activity. Most of the boiler-
plate code for Security, Object Relational Mapping, MVC framework, etc. is handled by the framework.
Teams using Java along with Spring can concentrate on the real business rather than wasting time on
rediscovering the wheels time and again.
3. Plenty of third party libraries, frameworks & IDE for faster development (IntelliJ, Eclipse, Spring Suite, etc)
4. Platform independent, write once run on most modern platforms (Linux, Unix, Windows, Mac, 32/64 bit
Hardware)
5. It supports Object Oriented Programming, thus making it easy to model real life scenarios into object model
6. In built support for multi-threading & concurrency, Its easy to write scalable applications in Java that
can utilize multi-core processors, clusters of machine, distributed RAM, etc. There is in built support for
Threads, ForkJoinTask (which deploys Work Stealing Algorithm), non-blocking algorithm using CAS
(Compare And Swap offers better scalability under moderate thread contention compared to traditional
locking mechanism), Java 8 Stream API, Parallel Streams, CompletableFuture, Parallel Array Operations,
Atomic Values, LongAccumulator, etc.
7. Very good support for Internationalization & Security.
8. Memory management is automatic by use of garbage collector (G1, Concurrent Mark Sweep, parallel
scavenge garbage collector, etc)
9. Pure Java byte code running on 32 bit JVM works perfectly fine on a 64 bit platform
10. Functional interfaces & lambda expressions introduced in Java 8 makes code writing an easy affair.
Specifically, dealing with Collections is fun in Java 8. For example, if you want to sort a collection of people
with last name, first name and e-mail (ignoring the case for e-mail), then the following code will do it all
Stream<Person> people = Stream.of(new Person(), ...);
people.sorted(Comparator.comparing(Person::getLastName)
.thenComparing(Person::getFirstName)
.thenComparing(Person::getEmail, Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)))
.forEach(System.out::println);
Java Cons
1. Is not a good fit for desktop applications because of heavy memory footprint and huge VM startup time
compared to a application written in low level language (C/C++, OS dependent native language). Two tools
written in Java namely Eclipse & Intellij IDEA are the exception.
2. Normal Java is not a good fit for real time systems because of its "stop the world garbage collector
pauses". Whenever Full GC triggers, it halts processing across the application for its duration of run.
JVM memory may cause larger System wide GC pauses, so the size of JVM should be decided keeping this
factor into account.
Please also note that 64 bit JVM requires more memory compared to 32 JVM for the same application
because now each reference starts consuming 64 bit instead of 32 bit i.e. management cost in 64 bit version is
higher than the 32 bit version. However, newer JVMs offer object pointer compression1 techniques which can
significantly reduce the space required by 64 bit JVM.
Oracle releases server JRE also, that contains normal JRE along with few tools (e.g. javac compiler) that are
required by Tomcat like servlet containers.
Encapsulation
Encapsulation is the mechanism of hiding of data
implementation by restricting access to public
methods. Instance variables are kept private and
accessor methods are made public to achieve
this.
Abstraction
Abstract means a concept or an Idea which is not associated with any particular instance. Using abstract class/
Interface we express the intent of the class rather than the actual implementation. In a way, one class should
not know the inner details of another in order to use it, just knowing the interfaces should be good enough.
Inheritance
Inheritances expresses "is-a" and/or "has-a" relationship between two objects. Using Inheritance, In derived
classes we can reuse the code of existing super classes.
In Java, concept of "is-a" is based on class inheritance (using `extends`) or interface implementation (using
`implements`).
Polymorphism
It means one name many forms. It is further of two types - static and dynamic. Static polymorphism is achieved
using method overloading and dynamic polymorphism using method overriding. It is closely related to
inheritance. We can write a code that works on the superclass, and it will work with any subclass type as well.
Java collections framework has an interface called `java.util.Collection`, `ArrayList` and `TreeSet` are two
different implementation of this interface. ArrayList maintains the insertion order of elements while `TreeSet`
orders its elements by their `natural order` or `comparator`(if supplied). Now if we write a method that accepts
a collection and prints its elements, the actual object (ArrayList or TreeSet) at runtime will decide the behavior
of this method.
public void print(Collection<String> collection) {
for (String s : collection) {
System.out.println("s = " + s);
}
}
Important facts
1. Other than objects of type `java.lang.Object`, all java objects are polymorphic i.e. they pass the IS-A test for
their own type as well as for class `Object`.
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 21
2. A reference variable's type determines the methods that can be invoked on the object that variable
is referencing to. In the example above, `print()` method can only invoke methods that are listed on
`Collection` interface irrespective the type of actual object passed to this method.
3. Polymorphic method invocation applies only to the instance methods (not to static methods, not to
variables). Only overriden instance methods are dynamically invoked based on the real object's type at
runtime.
Composition
Composition is stronger form of "has a" relationship where the composite object has sole responsibility for
disposition of its component parts i.e. in Composition, component parts share a lifespan with the composite
object - if the composite object is destroyed all its component parts must be destroyed. A perfect example
would be an Organization (composite object) having relationship with its Departments (component parts -
Finance, HR, Technical). Departments themselves do not have any independent existence outside the lifespan
of an organization.
Aggregation
This represents a weaker form of "has a" relationship between whole (aggregate) and the parts (component
parts). In this case the component parts may survive outside the lifespan of aggregate object and they may be
accessed without going through the aggregate object. Example could be Team and its Members, or department
and its members. Members may continue to exist outside the Team and even each member could be part of
multiple teams. Also the members can be accessed without accessing the team.
There is also a difference in the way these two relationships are represented in UML (UML is Object Oriented
Design Language) diagrams - Aggregation (weak relation) is represented by empty diamond while Composition
(strong relation) is represented by filled diamond as shown in the figure.
Compositions are a critical building block in many data structures, including Binary Tree and Linked List.
However even if "Composition" completely satisfies a particular given scenario (say, Caching), you may still
want to implement your design as an "Aggregation" due to technical challenges.
Composition is implemented using component part object Initialization inside constructor (or sometimes post
construction). All the component parts are destroyed as soon as composite object is destroyed, for example -
public class Organization {
private final List<Department> departments;
public Organization() {
departments = new ArrayList<>();
departments.add(new Department("Finance"));
departments.add(new Department("HR"));
departments.add(new Department("Technical"));
}
}
class Department {
private final String name;
Department(String name) {
this.name = name;
}
}
Other good examples are LinkedList composite object consisting of Nodes (component parts)
On the other hands, Aggregation relationship is expressed in code using the reference of component parts
inside the aggregate object, for example -
public class Team {
private final List<Member> members;
class Member {
private final String name;
Member(String name) {
this.name = name;
}
}
Here in this example, members are not destroyed when Team object is destroyed.
Other good examples could be a Pond object (aggregate object) and Duck objects (component parts), where
ducks may come for sometime in the pond for swimming.
User, etc) to resolve all our ambiguities. It could go really bad if we are not clear of software requirement, for
example lets consider these two scenarios -
Developer : What that data looks like and how much is the volume, where is it stored?
Business Analyst : Data consists of integers, volume could go as high as 4 GB residing in a file. User will
upload the file into the server and should get a sorted file in return.
Developer : What is the range of Numbers that need to be sorted, will there be duplicates?
Business Analyst : That data will consist of small numbers, all between range of 0 < number < 1 million, but
yes there will be lots of duplicates
Developer : What are the hardware specs for hosting this service?
Business Analyst : It will be 4 GB RAM, 8-core cpu, 160GB Disk machine, with a cluster of four such
machines
Developer : What are the Time and Space requirements for this service?
Business Analyst : It should run in minimal time possible and there could be multiple such requests running in
parallel.
So we can see how the requirement evolves after raising the relevant questions. Before asking these
questions developer could have implemented any sorting algorithm for this requirement without keeping in
mind the parallel processing, space requirements, etc.
The Dev team went ahead and provided the asked functionality without introspecting the real business cause
behind this requirement, later on came the real picture -
User actually wanted to manipulate the system data using some macro's in excel sheet and then wanted to
upload the same to the server, if proper questions had been asked the design would be different.
Whatever manipulation user wanted to perform, could have easily be done inside the server using Java
methods itself, so there was no real need for this change request raised by the user.
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 24
• It is easy to handle small numbers compared to very large numbers, when our motive is just to compare
them. Logarithm converts big values to small numbers.
• It makes multiplication and division of large numbers easy because adding logarithms is the same as
multiplying and subtracting logarithms is same as dividing.
• Logarithm is used in Computer Science for estimating the size of a given problem, time complexity of
an algorithm, space complexity of an algorithm, etc. For example,
i. A school may have 100-200 students which is 2 on log10 scale
ii. A University may have 1000-2000 students which is 3 on log10 scale
iii. A City may have population of 100, 000 which is 5 on log10 scale
iv. A State may have population of 1000, 000 which is 6 on log10 scale
v. A nation like India can have population of 1 Billion which is 9 on log10 scale
Thus its easy to deal with logarithmic scale when estimating a given problem for its size.
In pre modern era, when calculators were not there, logarithm tables were used for division and multiplication
of large astronomical numbers.
Notes
• Logarithm was used in India in ancient times around 2 BC to express astronomical units. It is known as
Laghuganak (लघुगणक) in Hindi.
• Logarithmic spirals are common in nature. Examples include the shell of a nautilus or the arrangement of
seeds on a sunflower.
2 http://simple.wikipedia.org/wiki/Logarithm
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 25
That is +5.
3 http://en.wikipedia.org/wiki/Two's_complement
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 26
Constant O(1) - a program whose running time's order of growth is constant, executes a fixed number of
operations to finish the job, thus its running time does not depend on N.
Linear O(N) - a program that spends a constant amount of time processing each piece of input data and thus
running time is proportional to the N.
Logarithmic O(log n) - a program where on every subsequent iteration, the problem size is cut by half, for
example - Binary Search.
Importance of Big O
We should always keep time efficiencies in mind while designing an algorithm for a data structures, otherwise
there could be severe performance penalties for using wrong algorithm for a given scenario.
PriorityQueue
O(1) for peek, element and size
O(log n) for offer, poll, remove() and add
O(n) for remove(Object) & contains(Object)
LinkedList
O(1) for removal and O(1) for add & poll method
O(n) for toString() method
Q 15. How would you determine Time Complexity of a given algorithm, are there any
general guidelines?
There are few rules which can help us in the calculation of overall running time of a given piece of code.
In case of if-else condition, we should include the time complexity of condition and if or else part, whichever is
larger.
2. Iterations and Loops - for, while and do-while (Multiply the Complexity)
Total time complexity can be calculated by multiplying the Time Complexity of individual statement with the
number of iterations. for example, in the below code
for (int i = 0; i < N; i++) { // N iterations
PriorityQueue.offer(i); // O(log k)
}
Q 16. What is a sorting algorithm? List down sorting algorithms by their time &
memory complexity in Big O notation? When do we call a sorting algorithm 'Stable'?
Sorting is an algorithmic technique to put all the collection elements in certain order1 i.e. numerical order,
lexicographical order (dictionary order), etc. Sorting is very helpful for solving real world problems for example,
data analysis requires searching which depends upon sorted input data.
Now there are two solution possible for the first two elements
1 http://en.wikipedia.org/wiki/Sorting_algorithm
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 29
OUTPUT1 --> [(1,2) (1,3) (2,3) (3,1)] --> stable sort because order is maintained
OUTPUT2 --> [(1,3) (1,2) (2,3) (3,1)] --> unstable sort because order changed from the original
Examples of Stable Sort algorithms are : Binary Tree Sort, Bubble Sort, Merge Sort, Insertion Sort, etc
Unstable Sorting Algorithms : Heap Sort, Selection Sort, Quick Sort
5. Adaptive Sort
An algorithm is called adaptive if it takes advantage of existing order in its input thereby reducing the overall
sorting time. Adaptive versions exists for heap and merge sort. For example, Java 8's iterative merge sort
method is adaptive to an extent that it requires approximately n comparisons if the input is nearly sorted.
Algorithms Summary
Below table assumes total n items to be sorted, with keys of size k, digit size d and range of numbers r
Algorithm Average Time Worst Time Space Stable Comparison Suitable for
Complexity Complexity Complexity Based? Memory
Quicksort O (n log n) n2 log n No Yes Internal
Binary Tree Sort O (n log n) n log n n Yes Yes Internal
Merge Sort O (n log n) n log n n Yes Yes External
Selection Sort O (n2) n2 1 (In Place) No Yes Internal
Bubble Sort O (n2) n2 1 (In Place) Yes Yes Internal
Heap Sort O (n log n) n log n 1 (In Place) No Yes Internal
Insertion Sort O (n2) n2 1 (In Place) Yes Yes Internal
Radix Sort O n.(k/d) n.(k/d) n+2d No No External
Counting Sort O (n+r) n+r n+r Yes No Internal
Question: Do you know what Sorting algorithm JDK uses for Java's Collections.sort(List<E>) method?
Java 8's Collections.sort(List<E>) uses Iterative merge sort algorithm, it requires fewer than n log(n)
comparisons when the input array is partially sorted (adaptive) and this algorithm is guaranteed to be stable in
nature.
5. Multiple Sort Criteria - Sort employees by first name and then by last name and print output to console
public void multiple_sort(List<Employee> employees) {
Comparator<Employee> byFirstName = (e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName());
Comparator<Employee> byLastName = (e1, e2) -> e1.getLastName().compareTo(e2.getLastName());
employees.stream()
.sorted(byFirstName.thenComparing(byLastName))
.forEach(e -> System.out.println(e));
}
Sorted Output 1 1 2 2 4 5 6
Java 8 example for Counting Sort
import java.util.Random;
public class CountingSort {
public static void main(String[] args) {
Random random = new Random(System.currentTimeMillis());
int min = 0;
int max = 1000;
int[] input = random.ints(min, max).parallel().limit(100000000).toArray();
CountingSort countingSort = new CountingSort();
countingSort.sort(input, min, max);
}
//Compute the total number of items occurring before the current item in sorted output (histogram)
for (int i = 1; i < counting.length; i++) {
counting[i] += counting[i - 1];
}
//Fill the output array with correct number of zeros, ones, twos and so on.
int[] output = new int[input.length];
for (int i : input) {
output[counting[i - min] - 1] = i;
--counting[i - min];
}
return output;
}
Layman usage in real life - A shopkeeper wants to sort the receipts at the year end from a box, he will simply
take a calendar and start putting receipts against the dates.
int current = 0;
for (int i = 0; i < counts.length; i++) {
Arrays.fill(a, current, current + counts[i], i + low); // fills counts[i] elements of value i + low in current
current += counts[i]; // leap forward by counts[i] steps
}
}
Note: we know that, given an array of integers, its maximum and minimum values can be always found; but if
we imagine the worst case for an array of 32 bit integers, we see that in order to hold the counts, we need an
array of 232 elements, i.e., we need, to hold a count value up to 232-1, more or less 4 Gbytes. So the counting
sort is more practical when the range is (very) limited and minimum and maximum values are known a priori.
(Anyway sparse arrays may limit the impact of the memory usage)
Bucket Sort Algorithm (Integer Sort with Time Complexity O (n) + distributed algorithm)
It is a distribution sort algorithm that works by partitioning (divide and conquer) an array into a number of
buckets, with each bucket sorted individually on the same machine or another using a different sorting
algorithm or by applying the same algorithm recursively.
If the number of buckets can't be made equal to the max value in the input data, then we can use the below
modified algorithm to sort the input data
public static int[] bucketSort(int[] array, int bucketCount) {
if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");
if (array.length <= 1) return array; //trivially sorted
int pointer = 0;
for (int i = 0; i < buckets.length; i++) {
Collections.sort(buckets[i]); //mergeSort
for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets
array[pointer] = buckets[i].get(j);
pointer++;
}
}
return array;
}
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 34
Q 17. Why Prime numbers are so important for certain algorithms like RSA &
hashcode?
Prime numbers are always unique and can not be divided by any other number except 1. Infact all integer
numbers (except 0 and 1) are made up of primes or their composites.
Prime numbers are very useful in generating hashcode, RSA algorithm and random number generators.
Hashcode Implementation
String class's hashcode method multiplies its hash value by prime number 31:
A number is either prime number or a composite number (can be factorized into prime numbers). The product
of prime number with any other number has the best chances of being unique (though not as unique as Prime
number itself) due to the fact that prime number is used to compose it. This property makes them very suitable
for use in hashing function so as to obtain fair distribution in its hashcode output and thus achieving low
collisions.
Multiplying by the prime number will not tend to shift information away from the low end, as it would multiplying
by a power of 2, thus achieving a fair randomness.
Q 18. What is left shift <<, right shift >> and Unsigned right shift >>> operator in
Java? How are these useful?
All Integer in Java are of signed type (negative numbers are represented in 2's complementary notation),
hence Java provides both signed and unsigned bit shift operators to support signed and unsigned shift of bits.
Unsigned right shift Operator >>> (does not respect sign of Number, does not preserve the 1st bit)
Notes
• Eight-bit type byte is promoted to int in shift-
expressions. To mitigate such effects we can
use bit masking to get the result as byte for
example, (b & 0xFF) >>> 2. Casting can also
help achieving the same.
• Uses of bitwise operators: bitwise operators are used for few very efficient mathematical calculations
in Big O(1). Bloom Filter, fast mathematical calculations, hashing functions of HashMap are some of
applications.
Q 20. How heap memory is divided in Java. How does Garbage Collector cleans up
the unused Objects? Why shouldn't we use System.gc() command in production code?
Memory taken up by the JVM is divided into Stack, Heap and Non Heap memory areas. Stacks are taken up
by individual threads for running the method code while heap is used to hold all class instances and arrays
created using new operation. Non-heap memory includes a method area shared among all threads and is
logically part of the heap but, depending upon the implementation, a Java VM may not invoke GC on this part.
The Young generation - This further consists of one Eden Space and two survivor spaces. The VM initially
assigns all objects to Eden space, and most objects die there. When VM performs a minor GC, it moves any
remaining objects from the Eden space to one of the survivor spaces.
Tenured/Old Generation - VM moves objects that live long enough in the survivor spaces to the "tenured"
space in the old generation. When the tenured generation fills up, there is a full GC that is often much slower
because it involves all live objects.
Metaspace - The metaspace holds all the reflective data of the virtual machine itself, such as class metadata,
classloader related data. Garbage collection of the dead classes and classloaders is triggered once the class
metadata usage reaches the “MaxMetaspaceSize”.
to collect the space from Young Generation First (very low performance overhead), but when we force our JVM
to invoke System.gc(), JVM will do a Full GC which might pause your application for certain amount of time,
isn't that a bad approach then? Let GC decide its timing.
Memory Spaces
Eden Space (heap): The pool from which memory is initially allocated for most objects.
Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden
space.
Tenured/Old Generation (heap): The pool containing objects that have existed for some time in the survivor
space.
Metaspace (non-heap): The pool containing all the reflective data of the virtual machine itself, such as
meta-data of classes, objects (e.g pointers into the heap where objects are allocated) and method objects,
classloader related data.
Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used
for compilation and storage of native code.
-XX:+DisableExplicitGC
Disable Sysytem.gc() which cause the Full GC to run and thus causing the JVM pauses.
-verbose:gc
-XX:+PrintGC
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
This will print every GC details
-XX:NewRatio
The ratio between the young space and the old is set by this parameter. For example, -XX:NewRatio=2,
would make old generation 2 times bigger than the young generation (ratio between the young and tenured
generation is 1:2), or we can say that the young generation is 1/3rd the size of total heap size(young + old)
-XX:SurvivorRatio
This command line parameter sets the ratio between each survivor space and eden. For example,
-XX:SurvivorRatio=6 will make each survivor space one eighth of the young generation. (there are two survivor
space and 6 eden spaces in this case, hence 1/8)
-XX:NewSize=n
Sets the initial size of young generation, it should typically be 1/4th of total heap size. The bigger the young
generation, the less frequent the minor collection happens. (though for a bounded heap size, it may cause
more frequent major collections)
-XX:MaxMetaspaceSize=128m
2 http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#generation_sizing.young_gen.survivors
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 38
Sets the maximum metaspace size (non-heap) to 128 MB which stores Classes, methods and other metadata.
We should carefully design the object pool because they fool the garbage collector by keeping the live
reference to the unused objects, thus causing application to demand more memory.
Default Values as of JDK 1.6 on server VM
Notes
Question: We have a application which creates millions of temporary large StringBuilder Objects from
multiple threads. But none of such object is really required after extracting useful information from
them. Somehow we started facing frequent gc pauses. What could be the problem, and how would you
approach it?
Solution
Performance tuning GC may solve this problem to some extent. Let's first understand memory requirements
of this application. This application create lots of short lived objects - thus we would require a large young
generation for lowering the frequency of minor garbage collection. If our young generation is small, then the
short lived objects will be promoted to Tenured Generation and thus causing frequent major collection. This can
be addressed by setting appropriate value for -XX:NewSize parameter at the JVM startup.
We also need to adjust the survivor ratio so that the eden space is large compared to survivor space, large
value of Survivor ratio should help solve this problem.
We can also try increasing the Heap size if we have sufficient memory installed on our computer.
Question : What are the available tools to give the visual view of the different memory spaces in a
3 http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#generation_sizing.young_gen.survivors
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 39
running JVM?
There are lot of free tools available for troubleshooting memory related problem in a JVM. JConsole and
JVisualVM are two of them that come shipped with every JDK. Below is the screenshot of JVisualVM (with
Visual GC plugin) showing the visual representation of the different memory segments for a running JVM.
You can always profile an application and see the memory trends and customize the memory allocations
accordingly. That can significantly reduce GC overhead and thus improve the application performance.
How do you interpret GC log message , like the one shown below?
8109.128: [GC [PSYoungGen: 109881K->14201K(139914K)] 691015K->595352K(1119040K), 0.0454530
secs]
• 107Mb used before GC, 14Mb used after GC, max young generation size 137Mb
• 675Mb heap used before GC, 581Mb heap used after GC, 1Gb max heap size
• minor GC occurred 8109.128 seconds since the start of the JVM and took 0.04 seconds
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 40
Q 21. What is difference between Stack and Heap area of JVM Memory? What is
stored inside a stack and what goes into heap?
The biggest difference between Heap and Stack section of memory is the lifecycle of the objects that reside in
these two memory locations
Memory of Stack Section is bound to a method context and is destroyed once a thread returns from the
function i.e. the Stack objects exists within the scope of the function they are created in.
On the other hand Heap objects exists outside the method scope and are available till GC recollects the
memory.
Java stores all objects in Heap weather they are created from within a method or class. Escape analysis can
be enabled in compiler to hint JVM to create method local objects in stack if the objects does not escape the
method context. All class level variables and references are also stored in heap so that they can be accessed
from anywhere. Metadata of classes, methods, etc also reside in Heap's PermGen space.
The Stack section of memory contains methods, local variables and reference variables and all os these are
cleared when a thread returns from the method call.
Question: An ArrayList is created inside a method, will it be allocated in Stack section or Heap section
of JVM Memory?
public void foo() {
ArrayList<String> myList = new ArrayList<>();
}
Answer : All Java Objects are created in Heap memory section, so the ArrayList will be created on the heap.
But the local reference (myList) will be created in the Stack section of memory. Once the method call is finished
and if myList variable is not escaped from this method then GC will collect the ArrayList object from heap.
As of JDK 1.6_14, escape analysis1 can be enabled by setting the appropriate JVM flag (java
-XX:+DoEscapeAnalysis) which hints the compiler to convert heap allocations to stack allocations if the method
local objects do not escape the method scope.
In the following code, if we enable the escape analysis, then the Object Foo may be created on Stack, resulting
in significant performance gain due to lesser GC activity.
Update: Escape Analysis is enabled by default as of Java 8, so you need not to do anything to enable it.
1 http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 41
Why to prefer Binary Tree over any other linear data structure?
Binary trees are a very good candidate (not the best) for storing data when faster search/retrieval is required
based on certain criteria. It does so by storing its elements in sorted order offering low time complexity for
retrieval operations compared to any other linear data structure. Any un-sorted collection can be inserted into
Binary Search Tree in O (n log n) time complexity. Though the insertion time is increased per element from
O(1) in Random Access array to O(log n) in Binary Search Tree, but we get a major advantage when we want
to search/retrieve a particular element from the tree data structure.
Worst-case Search time complexity is logarithmic in a balanced Binary Search Tree i.e. Binary tree cuts
down the problem size by half upon every subsequent iteration.
Red-black-tree is a height balanced binary tree where root is colored black and every other element is colored
either black or red with the following two rules,
1. If an element is colored red, none of its children can be colored red.
2. The number of black elements is the same in all paths from the root to the element with one child or with no
children.
It is useful for maintaining the order of elements in the collection based on the given comparator. It also provide
efficient mechanism to find the neighboring elements which are either big or small compared to given number,
because those numbers are stored physically closer in the data structure.
TreeSet is a navigable set implementation based on TreeMap. All the elements are ordered using their Natural
ordering or by comparator provided at TreeSet construction time.
NavigableSet provides us with methods like first(), last(), floor(), ceiling(), headSet(), tailSet() which can be
used to search the neighboring elements based on element's ordering.
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 42
TreeMap is Red-Black Binary Search Tree which guarantees logarithmic time for insertion, removal and
searching of an element. All the elements in this collection are stored in sorted order and the tree is height
balanced using Red black algorithm. If two elements are nearby in order, then TreeSet places them closely in
the data structure.
Uses
It is a best collection if we need to search the nearby elements of a given item based on their ordering.
Notes
• Note that this implementation is not synchronized. If multiple threads access a tree set concurrently, and at
least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished
by synchronizing on some object that naturally encapsulates the set.
• If no such object exists, the set should be "wrapped" using the Collections.synchronizedSortedSet method.
This is best done at creation time, to prevent accidental unsynchronized access to the set:
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
• If we are looking for high throughput in a multi-threaded application then we can prefer
ConcurrentSkipListSet which is scalable concurrent implementation of NavigableSet.
• Iterator returned by this class are fail-fast.
• TreeSet does not allow duplicates, it just replaces the old entry with the new one if both are equal (using
compareTo method)
• TreeSet does not preserve the insertion order of its elements.
• TreeSet provides guaranteed Big O (log n) time complexity for add(), remove() and contains() method.
There are multiple ways to handle session by a server framework. Most often, server uses one of the following
three mechanisms to handle session on server side -
Servlets use cookies as the default mechanism for session tracking, but in case cookies are disabled on the
client, Server can use URL re-writing for achieving the same. When server calls request.getSession(true), then
server generates and sends JSESSIONID back to the client for all future session references. JSESSIONID will
then be stored by the client and sent back to the server using any of the above mentioned mechanisms.
To ensure that your Servlets support servers that use URL rewriting to track sessions, you must pass all the
URL's used in your servlet through the HttpServletResponse.encodeURL() method, as shown below
Q 26. How can one handle relative context path while coding the web applica-
tions? For example, your web application may be deployed at a different context path
in Tomcat, how will you make sure static/dynamic resources works well at custom
context path?
static/images/a.gif
../static/images/a.gif
JSP EL
One can use pageContext variable available in session scope. Request contains contextPath variable which
points to the actual context path assigned to web application at the time of deployment.
${pageContext.request.contextPath}
In Freemarker
In your view resolver you can add the following property (mvc-dispatcher.xml)
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
<property name="requestContextAttribute" value="rc"/>
</bean>
Then in your freemarker template you can get the request context patch like
${rc.getContextPath()}
or, simply as
${rc.contextPath}
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 45
Q 28. How many elements a complete binary tree could hold for a depth of 10?
A binary tree is said to be complete if it is fully populated, so that each node has two child except the child
nodes.
From the figure shown, we can conclude that maximum
Nodes at level 0 = 1
Nodes at level 1 = 2
Nodes at level 2 = 4
Nodes at level n = 2n
Q 29. How will you swap two numbers without any temporary variable?
There are two approaches for swapping numbers without using any temporary variable.
1. Using sum of numbers
2. Using XOR operation (preferred)
void method1() {
int x = 10, y = 5;
x = x + y; // x becomes 15
y = x - y; // y becomes 15 - 5 = 10
x = x - y; // x becomes 15 - 10 = 5
System.out.println("After swap x = " + x + ", y = " + y);
}
The above code works as expected for smaller numbers, but there are chances of Integer overflow and
underflow when number is too large.
void method2() {
int x = 10;
int y = 5;
Q 30. Explain working of a hashing data structure, for example HashMap in Java.
HashMap is a hashing data structure which utilizes object's hashcode to place that object inside map. It
provides best case time complexity of O(1) for insertion and retrieval of an object. So it is a best suited data
structure where we want to store a key-value pair which later on can retrieved in minimum time.
HashMap is not a thread safe ADT, so we should provide necessary synchronization if used in multi-threaded
environment.
HashMap is basically an array of buckets where each bucket uses linked list to hold elements.
Initial Capacity
The default initial capacity of a hashmap is 16 (the number of buckets) and it is always expressed in power of
two (2,4,8,16, etc) reaching maximum of 1 << 30 (230)
bucket index = hashcode % (number of buckets) index =0 index=1 index = ... index=N
ConcurrentHashMap is similar in implementation to that of HashMap, with resizable array of hash buckets,
each consisting of List of HashEntry elements. Instead of a single collection lock, ConcurrentHashMap uses a
fixed pool of locks that form a partition over the collection of buckets.
HashEntry class takes advantage of final and volatile variables to reflect the changes to other threads without
acquiring the expensive lock for read operations.
The table inside ConcurrentHashMap is divided among Segments (which extends Reentrant Lock), each
of which itself is a concurrently readable hash table. Each segment requires uses single lock to consistently
update its elements flushing all the changes to main memory.
put() method holds the bucket lock for the duration of its execution and doesn't necessarily block other threads
from calling get() operations on the map. It firstly searches the appropriate hash chain for the given key and if
found, then it simply updates the volatile value field. Otherwise it creates a new HashEntry object and inserts it
at the head of the list.
Iterator returned by the ConcurrentHashMap is fail-safe but weakly consistent. keySet().iterator() returns
the iterator for the set of hash keys backed by the original map. The iterator is a "weakly consistent" iterator
that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed
upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to
construction.
Re-sizing happens dynamically inside the map whenever required in order to maintain an upper bound on hash
collision. Increase in number of buckets leads to rehashing the existing values. This is achieved by recursively
acquiring lock over each bucket and then rehashing the elements from each bucket to new larger hash table.
Question: Can two threads read simultaneously from the same segment in ConcurrentHashMap?
Answer: Segments maintain table of entry list that are always kept in consistent state, thus many threads
can read from the same Segment in parallel via volatile read access. Even the updates operations (put and
remove) may overlap with the retrieval operation without any blocking happening.
All these methods make concurrent programming a lot simpler than before, for example
• The below statement will conditionally create a new LongAdder() objects if none existed against the given
word and then increment the counter by One.
map.putIfAbsent(word, new LongAdder());
map.get(word).increment();
• The blow statement will print the entire key-value pair from the Hashmap (threshold is parallelism threshold
number beyond which multiple threads will execute the given operation)
map.forEach(threshold, (k, v) -> System.out.println(k + "->" + v));
• The below code snippet will increment the counter by one initializing to one if it is null
map.compute(word, (k, v) -> v == null ? 1: v+1);
• The below statement is another way of doing the same thing
map.computeIfAbsent(word, k -> new LongAdder()).increment();
• The below code snippet will search for the first match where value is greater than 100, returning null if
nothing found
String result = map.search(threshold, (k, v) -> v > 100 ? k : null) ;
• The below code snippet will count entries that have value > 100
Long count = map.reduceValues(threshold, v -> v > 100 ? 1L : null, Long::sum);
Q 32. Discuss Visitor, Template, Decorator, Strategy, Observer and Facade Design
Patterns?
Similarly
Calculating taxes in different regions on sets of invoices would require many different variations of calculation
logic. Implementing a visitor allows the logic to be de-coupled from the invoices and line items. This allows
the hierarchy of items to be visited by calculation code that can then apply the proper rates for the region.
Changing regions is as simple as substituting a different visitor.
continued on 51
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 51
@Override
public void visit(Employee emp) {
System.out.println(emp.getName());
}
}
@Override
public void accept(EmployeeVisitor visitor){
for (Employee employee : employees) {
visitor.visit(employee);
}
}
}
continued on 52
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 52
Another good example of facade design pattern could be : exposing a set of functionalities using web services
(SOA architecture). Client does not need to worry about the complex dependencies of the underlying system
after building such API.
SoftReference, WeakReference & PhantomReference are are reference-object classes, which supports limited
degree of interaction with the GC. A programmer may use these classes to maintain a reference to some other
object (referent) in such a way that the object may still be reclaimed by GC.
Reference Queues
Reference queue is used to track the objects claimed by GC. We can use the reference objects to check
whether the objects referred by these are still active or are claimed by GC.
SoftReference
If the strongest reference to an object is a soft reference then GC will not reclaim the object until the JVM is
falling short of memory, though it must be reclaimed before throwing an Out Of Memory Error. So the object will
stay longer than a weakly referenced object. It is mostly used for writing memory sensitive caches.
WeakReference
Is similar to soft reference with the only difference that it will be GC'ed in the next GC cycle if the strongest
reference to the object is a weak reference. When a weak reference has been created with an associated
reference queue and the referent becomes a candidate for GC, the reference object (not the referent) is
enqueued on the reference queue after the reference is cleared. The application can
then retrieve the reference from the reference queue and learn that the referent has been continued on 53
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 53
collected so it can perform associated cleanup activities, such as expunging the entries for objects that have
fallen out of a weak collection.
WeakHashMap
It is a HashMap that store its keys (not values) using WeakReferences. An entry in this map is automatically
removed when there is no other non-weak references to keys. This collection can be used to store associative
objects like transient object & its metadata, as soon as the object is claimed by the GC, the associated
metadata will also be removed by the map. Other application could be in a servlet environment where as soon
as the session expire's, clear all the session data/attributes.
PhantomReference
PhantomReference are garbage collected when the strongest reference to an object is a phantom. When an
object is phantomly reachable, the object is already finalized but not yet reclaimed, so the GC enqueues it in
a reference queue for post finalization processing. A Phantom Reference is not automatically cleared when it
is enqueued., so we must remember to call its clear() method or to allow phantom reference object itself to be
garbage collected. get() method always return null so as not to allow resurrect the referent object.
Phantom references are safe way to know an object has been removed from memory and could be thought of
as a substitute for finalize() method.
Automatically-cleared references
Soft and weak references are automatically cleared by the collector before being added to the queues with
which they are registered, if any. Therefore soft and weak references need not be registered with a queue
in order to be useful, while phantom references do. An object that is reachable via phantom references will
remain so until all such references are cleared or themselves become unreachable.
Reachability levels from strongest to weakest : strong, soft, weak, phantom. Java 6 docs states that -
• An object is strongly reachable if it can be reached by some thread without traversing any reference
objects. A newly-created object is strongly reachable by the thread that created it.
• An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference.
• An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing
a weak reference. When the weak references to a weakly-reachable object are cleared, the object
becomes eligible for finalization.
• An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized,
and some phantom reference refers to it.
• Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of
the above ways.
Notes
WeakHashMap is not a solution for implementing cache, SoftReference's could be better utilized for
implementing cache.
Applications of a WeakHashMap
WeakHashMap stores its keys using WeakReference, and can be used to map transient objects with their
metadata. Let's suppose we have a socket application which creates sockets on client's request and socket
lives there for sometime. Now if we want to associate some metadata with this socket such as identity of
the user, then WeakHashMap is a ideal container for storing such associative information. Since we are not
managing the lifecycle of the socket in this case, WeakHashMap will automatically remove all the metadata as
soon as the socket dies.
Applications of a SoftReference
Soft references can be used to build memory sensitive cache which automatically collects items as soon as the
cache is under high memory load, which otherwise has to be achieved by the programmer.
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 54
Dirty Read
Occurs when uncommitted results of one transaction are made visible to another transaction.
Unrepeatable Reads
Occurs when the subsequent reads of same data by a transaction results in seeing different values.
Phantom Reads
One transaction performs a query returning multiple rows, and later executing the same query again sees
some additional rows that were not present the first time.
We also call above three as Isolation Hazards, and the Transaction Isolation levels are related to these three
problems.
Isolation Level Dirty read Unrepeatable read Phantom read
Read Uncommitted Yes Yes Yes
Read Committed No Yes Yes
Repeatable Read No No Yes
Serializable No No No
For most of databases, the default Transaction Isolation Level is Read Committed.
(Read Committed does not see any inconsistent state of other transaction, with a fair amount of concurrency)
READ_UNCOMMITTED isolation level states that a transaction may read data that is still uncommitted by
other transactions. This constraint is very relaxed in what matters to transactional concurrency but it may lead
to some issues like dirty reads.
READ_COMMITTED isolation level states that a transaction can't read data that is not yet committed by other
transactions. But the repeated read within the same transaction may get different results.
REPEATABLE_READ isolation level states that if a transaction reads one record from the database multiple
times the result of all those reading operations must always be the same. This eliminates both the dirty read
and the non-repeatable read issues.
SERIALIZABLE isolation level is the most restrictive of all isolation levels. Transactions are executed with
locking at all levels (read, range and write locking) so they appear as if they were executed in a serialized way.
This leads to a scenario where none of the issues mentioned above may occur, but in the other way we don't
allow transaction concurrency and consequently introduce a performance penalty.
Please be noted that the above four isolation levels are in decreasing order of their concurrency. So for
scalability reasons, Serializable is rarely a good choice of design, as it offers only a single thread to work at a
given time.
Database Level
Isolation Level can be set at the DB level as per DB specifications.
Hibernate Level
We can set the default Isolation for all hibernate transaction using the below mechanism
<property name=”hibernate.connection.isolation”>x</property>
Where x is the transaction Isolation Number choosen from below option
public static final int TRANSACTION_NONE = 0;
public static final int TRANSACTION_READ_UNCOMMITTED = 1;
public static final int TRANSACTION_READ_COMMITTED = 2;
public static final int TRANSACTION_REPEATABLE_READ = 4;
public static final int TRANSACTION_SERIALIZABLE = 8;
Creating Index on database table column improves table query performance. It is a usual practice to create
Index on Table Columns that are frequently used in where clause of query. There is a slight drawback of
creating too many indexes on table columns that it has a performance hit on Table row insertion/updation since
that will require index updation as well, also they require additional space on Disc.
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 56
Q 37. How will you list all customers from customer table who have no Order(s) yet?
This is a typical problem that can be solved using Left Outer Join. As illustrated in the following figure, we want
to fetch the customers (colored area) who have not placed any orders yet.
Here we are using Left Outer Join (also called Left Join) on Orders with Customers to select all the
Customer(s) and then putting a where clause to eliminate the records without any Orders.
Another approach (but inefficient) to do the same could be to use sub-queries, as shown below:
select * from Customer c where c.id not in (select o.c_id from Order o)
Q 38. How would you fetch Employee with nth highest Age from Employee Table
using SQL?
Each row of Employee needs to be compared to very other row to fetch the above mentioned details, thus the
Time Complexity of this operation would be quite high (O (n2))
SELECT *
FROM Employee E1
WHERE (N-1) = (SELECT COUNT(DISTINCT(E2.Age))
FROM Employee E2
WHERE E2.Age > E1.Age)
Q 39. What is difference between Drop, Truncate and Delete commands in SQL?
Delete is used to delete rows from a table with optional where clause, we need to commit or rollback after
calling this operation. This operation will cause all DELETE triggers to be fired on the table.
DELETE FROM Employee WHERE age < 14;
Truncate removes all rows from table, this operation can not be rolled back and no triggers are fired, thus it is
faster in performance as well.
Truncate Table Employee;
Drop command will remove a table from the schema, all data rows, indexes, privileges will be removed, no
triggers will be fired and no rollback.
Drop Table Employee;
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 57
Q 40. What is Inner Join, Left Outer Join and Right Outer Join?
INNER JOIN
This is the most common and the default join operation. This join creates a resultset by combining the column
values of two tables (L and R) based upon the predicate. Each row of L (left table) is compared with each row
of R (right table) to find all pairs of rows that satisfy the join predicates. When the join-predicate is satisfied,
column values for each matched pair of rows of L and R are combined into a result row.
Example query is shown below.
SELECT *
FROM employee INNER JOIN department
ON employee.DepartmentID = department.DepartmentID;
SELECT *
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID;
OUTER JOIN
An outer join does not require each record in the two joined tables to have a matching record. The joined table
retains each record—even if no other matching record exists.
SELECT *
FROM employee LEFT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;
SELECT *
FROM employee RIGHT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;
References
http://en.wikipedia.org/wiki/Join_(SQL)
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 58
Non-Clustered Index
Non-Clustered Index contains pointers to the data that is stored in the data page. It is a kind of index backside
of the book where you see only the reference of a kind of data.
Clustered index usually provides faster data retrieval than the non-clustered index. Moreover clustered indexes
provides faster access to the contiguous rows because those rows are present physically adjacent in the actual
table.
References
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.sqlug/html/sqlug/sqlug537.htm
http://en.wikipedia.org/wiki/Database_index#Types_of_indexes
Q 42. How would you handle lazily loaded entities in web application development
using hibernate?
There are at least two approaches to handle problem of initializing lazily loaded objects in web layer.
1. Hibernate.initialize(<entity>) - this static factory method will Force initialization of a proxy or persistent
collection. This method should only be called inside the transaction otherwise it will throw exception. If we
are using Spring then we can write something like this
@Override
@Transactional(readOnly = false)
public TaskData findById(long id) {
TaskData taskData = taskDao.findById(id);
if (taskData != null) {
Hibernate.initialize(taskData.getTodoResources()); //TodoResources is lazy loaded object in TaskData entity
}
return taskData;
}
2. Incase of web applications, you can declare a special filter in web.xml, it will open session per request
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Depending upon the requirements, you can choose the best suited approach for your project.
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 59
When mapping entities with each other, we describe the relation among entities using OneToOne, OneToMany,
ManyToOne or ManyToMany mappings.
OneToOne
A Person has a PAN (Card) is a perfect example of One To One association.
Unidirectional - Person can refer to PAN entity
Bidirectional - PAN entity can refer back to Person
@Entity
public class Person {
@Id private int id;
@OneToOne
@JoinColumn(name="PAN_ID")
private PAN pan;
// ...
}
@Entity
public class PAN {
@Id private int id;
@OneToOne(mappedBy="pan")
private Person person;
// ...
}
OneToMany
A Person has many Skill(s), But a skill can not be shared among Person(s). A Skill can belong to utmost One
Person. One more example could be relationship between Employee and Department where an Department is
associated with Collection of Employee(s)
Unidirectional - A Department can directly reference Employee(s) by collection
Bidirectional - Each Employee has a reference back to Department
@Entity
public class Employee {
@Id private int id;
@ManyToOne
@JoinColumn(name="DEPT_ID")
private Department department;
// ...
}
@Entity
public class Department {
@Id private int id;
@OneToMany(mappedBy="department")
private Collection<Employee> employees;
// ...
}
Employee Table would keep DEPT_ID foreign key in its table, thus making it possible to refer back to Dept.
ManyToMany
One Person Has Many Skills, a Skill is reused between Person(s). One more example of this could be
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 60
relationship between Employee and Project. Each employee can work on multiple Project(s) and each Project
can be worked upon by multiple Employee(s). One more example could be relationship between Customer(s)
and Product(s) where One or More Customer(s) purchase many different Product(s) and Product(s) can be
purchased by different Customer(s)
Unidirectional - A Project can directly reference its Employee(s) by collection
Bidirectional - An Employee has Collection of Projects that it relates to.
@Entity
public class Employee {
@Id private int id;
@ManyToMany
private Collection<Project> projects;
// ...
}
@Entity
public class Project {
@Id private int id;
@ManyToMany(mappedBy="projects")
private Collection<Employee> employees;
// ...
}
Association or junction table is must to implement a ManyToMany relationship, this separate table connects
one line from Employee to one line from Poject using foreign keys. And each primary key of Employee and
Project can be copied over multiple times to this table.
Q 44. How would you implement ManyToMany mappings with the self entity in JPA?
We need to maintain two different mappings in the same entity for ManyToMany relationship as shown below -
@ManyToMany
@JoinTable(name="table_friends", joinColumns=@JoinColumn(name="personId"),
inverseJoinColumns=@JoinColumn(name="friendId"))
private Set<User> friends;
@ManyToMany
@JoinTable(name="table_friends", joinColumns=@JoinColumn(name="friendId"),
inverseJoinColumns=@JoinColumn(name="personId"))
private Set<User> friendOf;
In the above Bidirectional Mapping, One side of relationship will maintain the User's list of friends (friends), and
the inverse side of relationship will maintain how many people have this User in their friend list (friendOf).
@ManyToMany(mappedBy="friends")
private Set<User> friendOf = new HashSet<User>();
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 61
Single table inheritance is default, and table per class is optional so all JPA vendors may not support it.
JPA also defines mapped super class concept defined through the @MappedSuperClass annotation. A
Mapped Super Class is not a persistent class, but allows a common persistable mapping to be defined for its
subclasses.
Single Table Inheritance
In this inheritance, a single table is used to store all the instances of the entire inheritance hierarchy. The Table
will have a column for every attribute of every class in the hierarchy. Discriminator columns identifies which
class a particular row belongs.
Question: We want to extract common behavior in a super class in JPA entities but we do not want to
have table for that super class. How would you achieve this?
Answer - If we create a normal class as the super class, then as per JPA specifications, the fields for that class
are not persisted in the database tables. We need to create a super class extracting the common fields and
then annotate that class with @MappedSuperClass in order to persist the fields of that super class in subclass
tables. A mapped super class has no separate table defined for it.
References
http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
Q 46. How will you handle Concurrent updates to an database entity in JPA i.e. when
two users try to update the same database entity in parallel?
There are two main approaches to handle transaction concurrency using JPA 2.01
1. Optimistic Concurrency (Scalable Option) - This approach is as simple as adding a version column to
the database entity, as shown in the below code. When version column is present, JPA will increment
the version field for us upon every update to the row. Thus when two detached entities with the same
version try to update the database, one will fail (throws OptimisticLockException) because of mismatch
in version column value. This approach offer higher concurrency throughput compared to Pessimistic
Locking, because it does not serializes the thread access. This approach will work even for the detached
entities where a single database row was read in parallel by two threads, and later point in time these two
threads try to update the contents of detached database entities. This approach gives best performance for
applications with very less contention among the concurrent transactions.
1 https://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 62
2. Pessimistic Concurrency (badly-scalable) - In this approach, JPA will lock the database row (not object
in memory) when the data is read, and releases the lock upon completion of transaction. This way only
one database transaction can update the same entity at same time. In Oracle database, it's similar to the
following SQL statement -
(SELECT . . . FOR UPDATE [NOWAIT])
In Spring Framework, you can set this transaction Level by adding below annotation to the service method
Pessimistic approach works best for applications where contention ratio is high among the concurrent
transactions, otherwise it is a badly scalable option for handling concurrency.
@Entity
@Table(name="Patient", uniqueConstraints=
@UniqueConstraint(columnNames = {"firstName", "birthDate", "primaryIdentifier", "gender"}))
@TableGenerator(name="tbl-gen",
pkColumnName="PATIENT_SEQ", allocationSize=500,
table="hibernate_hilo")
public class Patient {
@GeneratedValue(strategy=GenerationType.TABLE, generator="tbl-gen")
@Id
private Long id;
@Entity
@Table(name="MyPatient", uniqueConstraints=
@UniqueConstraint(columnNames = {"firstName", "birthDate", "primaryIdentifier", "gender"}))
public class Patient {
@GeneratedValue(strategy=GenerationType.TABLE, generator="tbl-gen")
@GenericGenerator(name="table-hilo-generator", strategy="org.hibernate.id.TableHiLoGenerator",
parameters={
@org.hibernate.annotations.Parameter(value="hibernate_id_generation", name="table"),
@org.hibernate.annotations.Parameter(value="next_hi", name="column")
})
@GeneratedValue(generator="table-hilo-generator")
@Id
private Long id;
Q 49. How will you handle batch insert in hibernate for optimal usage of memory,
network and CPU?
A naive approach to insert 1M rows in the database using Hibernate might look like this -
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<1000000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
}
tx.commit();
session.close();
But when we try to run this code we may run into OutOfMemory exception and the performance of the method
will also be low.
Optimization Steps
• You will need to enable the use of JDBC batching in hibernate config file, for example batch size can be set
to 50
hibernate.jdbc.batch_size 50
• You can disable hibernate second level caching in hibernate config
hibernate.cache.use_second_level_cache false
However, this is not absolutely necessary, as we can explicitly set the CacheMode to disable interaction
with the second-level cache.
• Use batch insert code that clear the cache after flushing the records to database, as shown below -
Session session = sessionFactory.openSession();
session.setCacheMode(CacheMode.IGNORE);
Transaction tx = session.beginTransaction();
for ( int i=0; i<1000000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 50 == 0 ) { //50, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
Another good way of doing the same thing is to use Hibernate's Stateless Session
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 64
Q 50. How will you operate on records of a large database table with million of entries
in it using Hibernate?
We can use StatelessSession for this purpose which gives very good performance using optimal system
resources.
Let's say we want to stream all records from a huge database table and scan for duplicate files -
public void analyzeAllDuplicates() {
StatelessSession session = getSessionFactory().openStatelessSession();
Transaction tx = session.beginTransaction();
ScrollableResults fileDocuments = session
.getNamedQuery(FileDocument.GET_ALL_DOCUMENTS)
.scroll(ScrollMode.FORWARD_ONLY);
while (fileDocuments.next()) {
FileDocument document = (FileDocument) fileDocuments.get(0);
checkAlreadyExists(document);
// document.updateStuff(...);
// session.update(document);
}
tx.commit();
session.close();
}
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 65
Q 51. Do you think Hibernate's SessionFactory and Session objects are thread safe?
SessionFactory is thread-safe and in normal application we should just create one SessionFactory object per
classloader per JVM. SessionFactory class stores all the second level cache and query cache and thus is very
heavy weight object.
Session on the other hand should be created per transaction, as it is not thread-safe.
A typical transaction should use the following idiom:
Q 52. What is difference between Hibernate's first and second level cache?
Hibernate's first level cache resides at Session Level. Session cache caches object within the current session
but this is not enough for long level i.e. session factory scope.
Hibernate's second level cache resides at Session-factory level. This cache exists as long as the session
factory is alive.
Important point about Hibernate's first and second level cache is that it only works when we fetch records by
ID, for non-id fields cache does not come into picture at all.
session.get()
This method will actually hit the database and return the real object along with its properties. It is a EAGER
loaded operation that requires round trip to database. If no record is found for the given ID, then null will be
returned.
session.load()
This method returns a reference (proxy) to the entity with the given identifier. This method does not hit the
database until a property on the return proxy is accessed for the first time. If no row is found, it will throw an
ObjectNotFoundException.
Q 54. What is usecase for GET, PUT, POST and DELETE method in REST API?
GET
To retrieve a resource, you shall use HTTP GET. We should not modify the state of resources using HTTP GET
method ever.
POST
To create a resource on the server, use HTTP POST. An example could be new user registration REST
endpoint, it will create a new user in the system.
PUT
To change the state of a resource or to update it on the server, use PUT
DELETE
To remove or delete a resource on server, use DELETE
Redirect
1. A redirect is a two step process where web application instructs the browser client to fetch the fetch the
second URL which differs from the original.
2. Server sends Http Status Code of 301 to the client, and then client follows the instructions.
3. If someone reloads the page on browser, then original request will not be repeated. Just the second url will
be fetched again.
4. Redirect is marginally slower than forward, since it requires two requests.
5. Objects placed in request scope are not available to second request.
6. There are several ways to perform a redirect for example,
Q 57. What are the best practices for handling TimeZone in database transactions?
There are multiple ways to handle Time Zone in your Java Application which deals with database transactions -
1. While using PreparedStatement, we should always prefer setDate(int parameterIndex, Date date, Calendar
cal) method to specify the Calendar in desired time zone.
2. For Spring JDBCTemplate, we should pass Calendar (with desired TimeZone) instance instead if plain Date
object.
3. We can also set application wide TimeZone using TimeZone.setDefault(TimeZone.getTimeZone(String ID))
4. JVM wide time zone can be set by passing JVM argument -Duser.timezone=GMT
Do's -
1. Prefer JodaTime API for handling TimeZone specific calculations in your application. JodaTime provides
simple and better api's for playing with Date & TimeZone.
2. While persisting time in your application, always prefer to use GMT or any other TimeZone which is not
affected by the Day Light Savings. And always include the original timezone name while storing the date so
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 68
that you can easily re-construct the date to the same value.
3. Business rules should always work on GMT time.
4. Only convert to local time at the last possible moment.
5. TimeZones and Offsets are not fixed and may change in future, always design your application keeping this
thing in mind.
Don't -
1. Do not use javascript based Date and Time Calculations in your web applications unless absolute
necessary as time and date on client machine may be different or incorrect.
2. Never trust Client DateTime on your server application.
3. Do not compare client datetime with server datetime.
Q 58. How will you check the owner information of a given domain name in web?
There is a Domain information lookup utility named whois (provided by sysinternals). it will list all the
information related to a given domain name.
whois www.google.com
Connecting to IN.whois-servers.net...
Domain ID:D8357-AFIN
Domain Name:GOOGLE.CO.IN
Created On:23-Jun-2003 14:02:33 UTC
Last Updated On:22-May-2014 09:17:32 UTC
Expiration Date:23-Jun-2015 14:02:33 UTC
Sponsoring Registrar:Mark Monitor (R84-AFIN)
Status:CLIENT DELETE PROHIBITED
Status:CLIENT TRANSFER PROHIBITED
Registrant ID:mmr-108695
Registrant Name:Christina Chiou
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 69
Q 59. What happens when you type www.google.com in your browser's address bar
from an Indian Location?
Skills - HTTP protocol, DNS Lookup, Networking, TCP connection, etc
There are series of events that happen when we type in www.google.com into browser's address bar from a
given location, we will cover few main steps here -
1. User enters www.google.com into the address bar
2. Browser checks if the typed address is www url or the search term, if it is search term then it will use pre
configured web search server (may be google or bing, etc) to search the typed term from web.
3. If the requested Object is in browser's cache and cache is valid, content is rendered from cache, otherwise
4. DNS Lookup takes place - Browser resolves the IP address for the mentioned server (www.google.com)
i. It checks in the browser cache
ii. checks the OS Cache
iii. checks the router cache
iv. checks the ISP cache
v. DNS recursive search until it reaches authoritative DNS Server. If multiple ip addresses are found
for a given server address, then DNS Round Robin algorithm picks up any one for the further
communication. If it does not recognize the domain then it gives error message.
we can use <nslookup www.google.com> command on windows to check what all IP addresses are
mapped to this www.google.com domain, incase they are multiple DNS server will use round robin
algorithm to pick up any one from the list.
5. Browser initiates TCP connection with the IP address and issues HTTP GET request to the server, it
passes along an HttpRequest that includes metadata about browser, user preferences (language, locale
etc.) and cookies for that domain.
6. Google.com server receives the request, uses the passed information (cookies) to find who the user is,
locale, language and region and sends http redirects (HTTP GET 302) to browser to use local regional
google server, i.e. www.google.co.in in our case (temporarily redirect)
7. Browser receives the response (302 in this case) and sends a fresh request to the newly mentioned
location in the previous response, passing the user information again (cookies for that domain, metadata,
etc)
8. Google.co.in receives the request the decodes the user and send the appropriate HTML response including
headers (status code 200 OK, content type, etc)
9. The Browser receives the response and begins to parse it for display. if it is compressed, browser will
decompress it, The HTML body will include links to css, images, js. All these links will trigger additional calls
back to server to retrieve those files. CDN (Content Delivery Networks) may serve these static resource
requests to speedup the process.
10. Browser layout engine will start to assemble the final page for display. css, js information may alter the
layout of the page.
11. The final page is assembled and rendered to the user.
12. After this the browser may send further AJAX request to communicate with the web server even after the
page is rendered.
ping www.google.com
Pinging www.google.com [74.125.20.105] with 32 bytes of data:
Reply from 74.125.20.105: bytes=32 time=244ms TTL=41
we can see here that ping uses just one IP address while nslookup shows multiple ip addresses mapped by a
given domain name.
Useful Links -
http://edusagar.com/articles/view/70/What-happens-when-you-type-a-URL-in-browser
https://technet.microsoft.com/en-in/library/cc772774(v=ws.10).aspx
https://github.com/alex/what-happens-when#check-hsts-list
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 71
Bean Factory is actual representation of the Spring IOC container that is responsible for containing and
managing the configured beans. XmlBeanFactory is the commonly used BeanFactory implementation.
Q 64. What are different types of Dependency Injection that spring support? or in
other words what are the ways to initialize beans in Spring?
• Constructor Injection : Dependencies are provided as constructor parameters.
<bean id="authenticator" class="com.mycompany.service.AuthenticatorImpl"/>
We should constructor injection for all mandatory collaborators and setter injection for all other properties.
We should use the prototype scope for all beans that are stateful and the singleton scope should be used for
stateless beans.
Q 67. How will you load hierarchy of property files in Spring Context?
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:**/default.properties</value>
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 73
<value>classpath*:**/database-${env:qa}.properties</value>
<value>classpath*:**/${env:qa}.properties</value> <!--more specific properties goes here-->
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
Please note that property files supplied later have higher precedence and will override the existing properties
with same name in earlier files.
Q 68. How to handle Bean Post Initialization and Pre Destroy Tasks in Spring Frame-
work? For example resource loading after bean construction and resource cleanup
before shutdown of spring context?
Spring framework provides two convenient annotations to handle Bean initialization and cleanup -
• @PostConstruct - annotate this on a method that will initialize a bean post its construction.
• @PreDestroy - annotate this on a method that will do cleanup before Application context closes.
@PostConstruct
public void loadSettings() throws InstantiationException, IllegalAccessException {
System.out.println("Loading Settings File");
appContext = Utils.load(AppContext.class, "file-searcher.xml");
}
@PreDestroy
public void saveSettings(){
System.out.println("Saving Settings File");
Utils.save(AppContext.class, appContext, "file-searcher.xml");
}
}
Whenever Spring container starts, method with @PostConstruct annotation will be invoked. And whenever
we call context.close(), method with @PreDestroy annotation will be invoked. Below is the typical standalone
method to start and stop the Spring Application Context.
System.in.read();
context.close();
}
}
Q 70. How will you embed a PNG/GIF image inside a CSS file?
Base64 encoded images can be placed inline into a CSS file, most browsers support this feature. We can use
Java 8's Base64 encode() and decode() method to convert an image into Base64 and vice versa.
Then the encoded PNG image can be placed inline into the required CSS file as shown below -
<style type="text/css">
input:required:valid { background-image: url(data:image/png;base64,<base64 contents goes here>);
background-position: right top; background-repeat: no-repeat; }
</style>
Chapter - Concepts Cracking Java Interviews (Java 8, Hibernate & Spring) 75
What is ParallelStream?
Java provides two main functionalities out of the box -
a) Partitioning of the stream of data into smaller streams to make parallel processing possible.
b) Parallel processing of the data
Creating a Stream
Streams can be created in multiple ways -
• Stream from Values
Stream<String> stream = Stream.of("Ken", "Jeff", "Chris", "Ellen");
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
IntStream oneToFive = IntStream.range(1, 6);
• Find first line from log file that contains word 'password
Stream<String> lines = Files.lines(path);
Optional<String> passwordEntry = lines.filter(s -> s.contains("password")).
findFirst();
.map(Path::getFileName)
.forEachOrdered(System.out::println);
}
• Joining Collections to make a Single String -
String ids = String.join(", ", ZoneId.getAvailableZoneIds());
List<String> nameList = asList("first", "second","third");
String names = String.join(", ", nameList);
If the input collection is not of type string, then a map function can be provided to map a given object to String
using some method e.g. toString() in this case.
Q 73. How will you replace tokens in a given text with properties loaded from a
property file using Java Regular Expressions?
We can use java regex to find and replace special tokens from a given text.
We can switch on case insensitiveness of pattern by providing Pattern.CASE_INSENSITIVE in the Pattern.
compile() method.
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
If we just want to replace all 'cat' words with 'dog' in a given paragrapgh (i.e. no special token recognizer), then
we can use the below Java Program
public void replaceAllCatWithDog() {
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
}
Q 74. How will you configure custom sized ThreadPool for Java 8 Stream API parallel
operations?
Java does not provide any direct mechanism to control the number of threads and ThreadPool used by
parallel() method in stream API, but there are two indirect way to configure the same.
For example, set the System property before calling the parallel stream
long start = System.currentTimeMillis();
IntStream s = IntStream.range(0, 20);
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20");
s.parallel().forEach(i -> {
try {
Thread.sleep(100);
} catch (Exception ignore) {
}
System.out.print((System.currentTimeMillis() - start) + " ");
});
"Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using
the ForkJoinPool.commonPool() if not in ForkJoinPool()"
class StreamExampleJava8 {
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
ForkJoinPool forkJoinPool = new ForkJoinPool(4); // Configure the number of threads
forkJoinPool.submit(() -> IntStream.range(1, 1_000_000).parallel().filter(StreamExampleJava8::isPrime).boxed().
collect(toList())).get();
forkJoinPool.shutdown();
}
Chapter 2
Core Java Interview Questions
Q 75. What are new features added in Java 8?
• Lambda Expressions enable us to treat functionality as the method argument, or say code as data. These
expressions make single-method interface more compact. for example
Single-method interface usage has become compact as seen in below code snippet
Before Java 8,
Thread tOld = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Munish Chandel");
}
});
Now, in Java 8
Thread t = new Thread(() -> System.out.println("Munish Chandel"));
The above code snippet filters collection of people who are Male and in age range of 18-25, get their
e-mail address and print them on System.out, few other examples of lambda expression to create a map of
Person based on their Gender, using sequential and parallel approach
Map<Person.Sex, List<Person>> byGender =
roster.stream().collect(Collectors.groupingBy(Person::getGender));
• Default methods enable new functionality to be added to the interfaces of libraries and ensure binary
compatibility with code written for older versions of those interfaces.
• Optimistic Locking in Code using Stamped Locks provides very lightweight synchronization
• Concurrent Adders & Accumulators - DoubleAdder, DoubleAccumulator, LongAdder, LongAccumulator
• Array Parallel Sorting API
• Complete New Date API
• Functional Interfaces
• Stream API in Collections enables bulk operations, such as sequential and parallel map-reduce functions
• Improvements in ConcurrentHashMap - added methods for atomic & bulk operations
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 83
Method Signature
Method name, plus the number and type of its parameters constitute the method signature. Return type is not
part of method signatures.
Hiding Variables
Overriding works for Instance methods, In case of Class methods If a subclass defines a class method
with the same signature as a class method in super class, the method in subclass hides the one is
super class.
Similarly variable names are never overridden by the sub class, but they can be hide. If the super class
contains a variable named x and subclass also contains x (irrespective of the type of variable) then the
subclass variable hides the super class variable. Remember that all non-private super class variable can
always be referenced by the subclass using super.variable.
In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods
neither hide nor override the superclass methods—they are new methods, unique to the subclass.
1 SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) Page 106
2 http://docs.oracle.com/javase/tutorial/java/IandI/override.html
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 84
Question : If a method throws FileNotFoundException in a super Class, can we override this method in
sub-class by throwing IOException?
Answer : FileNotFoundException extends IOException, and both of these are Checked Exceptions. So As
per overriding rules, sub-class method can only narrow down the scope of Exception i.e. overriding method
can only throw the same Exception or sub-classes of that Exception. So overriding method can not throw
IOException in this case.
Compiler introduces the call to super() only if the default no-arg constructor is present in the super class, otherwise
its responsibility of the programmer to introduce such call with proper constructor arguments.
Java Source
class A {
A() {
greeting();
prints();
}
void greeting() {
System.out.println("instance method from A");
}
static void prints() {
System.out.println("Static method from A");
}
B() {
/*Compiler will automatically insert calls to no-args A's constructor using super();*/
greeting();
prints();
}
void greeting() {
System.out.println("instance method from B");
}
Program Output
instance method from B
Static method from A
instance method from B
Static method from B
class C extends A {}
class D extends B, C {}
In the code above, the implementation of method `foo()` inherited by class D is unambiguously that defined
by class A. But as soon as class B and class C starts to provide its own implementation for method `foo()`,
the ambiguity will arrive in method resolution by Class D. This trap is known as diamond problem of multiple
inheritance.
Since Java does not allow multiple inheritance for classes (only multiple interfaces are allowed), so diamond
problem can not exist in Java. At any given point in time, a given Java class can extend from only one super
class.
Rule 1
Any method inherited from a class or a superclass is given higher priority over any default method inherited
from an interface.
In the diagram above, `foo()` method of class D will inherit its implementation from class C.
Rule 2
Derived interfaces or sub-interfaces take higher precedence
than the interfaces higher-up in the inheritance hierarchy.
Rule 3
In case Rule 1 and Rule 2 are not able to resolve the conflict then the implementing class has to specifically
override and provide a method with the same method definition.
In above class diagram, since interface A & B are at same level, to resolve conflict, class C must provide its
own implementation by overriding method `foo()`.
Q 80. When should we choose Array, ArrayList, LinkedList over one another for a
given Scenario and Why?
LinkedList (Doubly-linked list) and ArrayList (Resizable-array) both are two different implementations of List
Interface.
LinkedList
LinkedList provides constant-time (Big O(1)) methods for insertion and removal using Iterators. But the
methods to find the elements have Big O(n) time complexity (Linear Time, proportional to the size of list)
and thus are poor performing. LinkedList has more memory overhead because it needs two nodes for each
element which point to previous and next element in the LinkedList. If you are looking for random access of
elements then ArrayList is the way to go for.
ArrayList
ArrayList on the other hand allows Big O(1) time complexity (constant time) for read/update methods. If
position of the element is known then it can be grabbed in constant time using get(index) operation. But adding
or removing elements from ArrayList (other than at end) requires shifting elements, either to make a new
space for the element or for filling up the gap. Thus if frequent insertions and removals are required by your
application logic then ArrayList will perform poorly (roughly Linear Time Big O(n)). The size, isEmpty, get, set,
iterator, and listIterator operations run in constant time. Also if more elements are needed than the capacity of
the ArrayList then a new underlying array with twice the capacity if created and the old array is copied to the
new one which is time consuming operation (roughly Big O(n)). To avoid higher cost of resizing operation, we
should always assign a appropriate initial capacity to the ArrayList at the time of construction.
Array
Array is a fixed size primitive collection which can hold primitive or Objects. Array itself is a object and memory
for array object is allocated on the Heap. Array does not provide useful collections methods like add(), addAll(),
remove, iterator etc.
We should choose array only when the size of input is fixed and known in advance and underlying elements
are of primitive type.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 88
public class A {
void foo() {System.out.println("Foo A");}
}
class B extends A {
void foo() {System.out.println("Foo B");}
}
class C extends B {
void foo() {
System.out.println("Foo C");
}
Now its possible for B and C to call their super class's foo() method by using super.foo() call.
But the interviewer is asking us to invoke A's foo() method from Class C, which is not possible because it
violates the OOPs concept in Java. Java does not support multiple inheritance, that means C can see only a
single super class which will have just one foo() method implementation. C can never see A's foo() method
because otherwise how would it know which foo() method to invoke - B's or A's
class B extends A {
void foo() {super.foo();}
}
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 89
Q 82. Why wait is always used inside while loop as shown in the below snippet?
Discuss all the probable reasons.
public synchronized void put(T element) throws InterruptedException {
while(queue.size() == capacity) {
wait();
}
queue.add(element);
notify();
}
There are two main reasons that force use to use wait() method inside a while loop1.
Spurious WakeUp2
In certain rare scenarios, a thread can wakeup without any reason even when no other thread signaled the
condition. To gracefully handle those scenarios, we must recheck for the required condition before proceeding
to execute the rest of the condition dependent code.
Replacing if condition with a while loop can solve this problem without much effort. While loop will force each
resuming thread to test the condition on wakeup, and putting the thread to waiting state again if required
condition is not met.
So always remember to use wait() method from inside the while loop testing the condition that caused the
thread to awaken, as shown below.
synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout);
... // Perform action appropriate to condition
}
1 see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" , or Item 50 in Joshua Bloch's "Effective
Java Programming Language Guide" (Addison-Wesley, 2001)
2 http://en.wikipedia.org/wiki/Spurious_wakeup
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 90
Q 83. We have a method which iterates over a Collection. We want to remove certain
elements from that collection inside the loop in certain criteria is matched, How should
we code this scenario?
Intent here is to check if you are aware of technique of modifying the collection structure while iterating over it.
If we call collection.remove() from within the for loop then ConcurrentModificationException will be thrown by
the JVM at runtime.
Actually, the right way to handle such scenario is to use Iterator to remove the element from the underlying
Collection while iterating over it. ConcurrentModificationException is thrown because the for loop internally
creates a fail-fast iterator which throws exception whenever it finds any structural modification in the underlying
data structure (ArrayList in this case).
The correct Java 7 implementation for removal method would look something like,
public void removeFromCollection(List<Integer> marks) {
for (Iterator<Integer> iterator = marks.iterator(); iterator.hasNext(); ) {
Integer mark = iterator.next();
if (mark < 40)
iterator.remove(); //==> Safe to call remove() on Iterator
}
}
Java 8 provides us with removeIf(Predicate) feature to remove objects from a collection, for example to remove
all integers that are larger than 15, we can use the following code
public void removeFromCollection() {
List<Integer> collect1 = Stream.of(10, 20, 30, 40).collect(toList());
collect1.removeIf(integer -> integer > 15);
collect1.forEach(System.out::println);
}
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 91
This question is based on the fundamentals explored in the last question. If we try to modify a Collection inside
a for loop without using an explicit Iterator then ConcurrentModicifactionException is thrown. So will not repeat
the same wrong code in this solution.
Unfortunately Iterator does not provide any add() method in its interface, so it would be hard to use Iterator in
this API to structurally modify the data structure. We are left with two options here -
1.) Use ListIterator's add() method which works only for LinkedList as the underlying data structure rather than
any Collection.
2.) Create another List and add stuff to that while we iterate over the input collection, and in the end append all
elements of this newly created List to the original Collection.
public void addIntoCollection2(LinkedList<Integer> marks) {
List<Integer> tempFooList = new ArrayList<Integer>();
for (Integer mark : marks) {
if (mark < 40)
tempFooList.add(mark);
}
marks.addAll(tempFooList);
System.out.println("marks = " + marks);
}
Q 85. If hashcode() method of an object always returns 0 then what will be the impact
on the functionality of software?
Hashcode is used to fairly distribute elements inside a map into individual buckets. If the hashcode returned
is zero for each element then the distribution will no more be fair and all the elements will end up into a single
bucket. Each bucket in a HashMap contains list of HashEntry objects, so in a way HashMap will act as a map
with single bucket holding all of its elements in a list. That will drastically reduce HashMap's performance to
that of a LinkedList for get and put operations.
So time complexity of get and put method will become : Big O(n) instead of Big O(1)
Although, functionally it will still behave correctly.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 92
Q 86. Iterator interface provides remove() method but no add() method. What could
be the reason for such behavior?
Iterator interface contains three methods namely remove(), hasNext() and next().
It intentionally does not provide any add() method because it should not !
Iterator does not know much about the underlying collection. Underlying collection could be of any type (Set,
ArrayList, LinkedList, etc) and might be offering the guaranteed ordering of its elements based on some
algorithm. For example TreeSet maintains the order of its element using Red Black Tree datastructure. Now
if iterator tries to add an element at a given location, then it might corrupt the state of the underlying data
structure. And that is not the case while removing elements.
Thus Iterator does not provide any add() method.
List Iterator does provide the add() method because it know the location where it needs to add the newly
created element as List preserves the order of its elements.
It provides an immutable view to mutable collection, its not possible to modify the underlying collection through
methods of this object.
Q 88. If we don't override hashcode() while using a object in hashing collection, what
will be the impact?
Then the Object's default hashcode() method will be used to calculate the hashcode, which in turn will return
the memory address of the object in hexadecimal format. So in a way the hashmap will behave like a identity
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 93
hashmap which will consider two elements equal if and only if two objects are same as per their memory
address (and not logically). For example two String Objects with same contents might be treated different by
this hashmap if they are different on heap.
This will list down the thread ids for the troubleshooting purpose.
Q 91. What is difference between Vector and ArrayList, why should one prefer
ArrayList over Vector?
Main differences between the two dynamic List data structures are -
1. Vector methods is synchronized, ArrayList is not
2. Data Growth - Vector doubles its size when its full, ArrayList increases its size by 50% when its full.
3. Vector class retrofitted to implement the List interface, making it a member of the Java Collections
Framework in JDK 1.2
4. Stack is sub-class of Vector class
Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations
is useless for practical purpose (if you iterate over a Vector, for instance, you still need to take out a
lock to avoid anyone else changing the collection at the same time, which otherwise would cause a
ConcurrentModificationException in the iterating thread) and also slower due to repeated locking - once at
individual method level inside Vector another at block level in your code.
In almost all scenario’s you can utilize ArrayList in your application code, if you are looking for a synchronized
version of List you can decorate a collection using the calls such as Collections.synchronizedList()
In the above code, two threads operate over two shared Resources r1 and r2. Resource class has two
synchronized methods (which will require the threads to obtain lock over the instance) and unfortunately r1
has a inter-dependency on r2. There is a great probability that the above code will block for ever causing a
deadlock.
Using jconsole we can detect the deadlock, below is the message shown in jconsole for this java process
Name: Thread-1
State: BLOCKED on org.shunya.power.interview.DeadLock$Resource@354949 owned by: Thread-0
Total blocked: 2 Total waited: 1
Name: Thread-0
State: BLOCKED on org.shunya.power.interview.DeadLock$Resource@661a11 owned by: Thread-1
Total blocked: 1 Total waited: 1
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 95
Q 93. Which data type would you choose for storing currency values like Trading
Price? What's your opinion about Float, Double and BigDecimal?
Float & Double are bad for financial world, never use them for monetary calculations.
• All floating point values that can represent a currency amount (in dollars and cents) can not be stored
exactly as it is in the memory. So if we want to store 0.1 dollar (10 cents), float/double can not store it as it
is. Let's try to understand this fact by taking this simple example
public class DoubleForCurrency {
public static void main(String[] args) {
double total = 0.2;
for (int i = 0; i < 100; i++) {
total += 0.2;
}
System.out.println("total = " + total);
}
}
• There is not much flexibility provided by Math.round() method for rounding the given calculation result
compared to functionality offered by MathContext. RoundingMode provides options such as ROUND_UP,
ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_UNNECESSARY, etc
That's the reason we should always prefer BigDecimal or BigInteger for financial calculations.
Notes
Primitive type - int and long are also useful for monetary calculations if decimal precision is not required
We should really avoid using BigDecimal(double value) constructor instead prefer BigDecimal(String) because
BigDecimal (0.1) results in 0.100000...5..3 being stored in BigDecimal instance. In contrast BigDecimal ("0.1")
stores exactly 0.1
For example, 12.345 has precision of 5 (total digits) and scale of 3 (number of digits right of the decimal)
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 96
How to format BigDecimal Value without getting exponentiation in the result & Strip the trailing zeros?
We might get exponentiations in the calculation result if we do not follow some best practices while using
Bigdecimal. Below is the code snippet which shows a good usage example of handling the calculation result
using Bigdecimal.
import java.math.BigDecimal;
public class BigDecimalForCurrency {
public static void main(String[] args) {
int scale = 4;
double value = 0.11111;
BigDecimal tempBig = new BigDecimal(Double.toString(value));
tempBig = tempBig.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
String strValue = tempBig.stripTrailingZeros().toPlainString();
System.out.println("tempBig = " + strValue);
}
}
How would you print a given currency value for Indian Locale (INR Currency)?
NumberFormat class is designed specifically for this purpose. Currency symbol & Rounding Mode is
automatically set based on the locale using NumberFormat. Lets see this example
public static String formatRupees(double value) {
NumberFormat format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(5);
return format.format(value);
}
Some precautions
• BigDecimal(String) constructor should always be preferred over BigDecimal(Double)
• Convert Double value to string using Double.toString(double) method
• Rounding mode should be provided while setting the scale
• StripTrailingZeros chops off all the trailing zeros
• toString() may use scientific notation but, toPlainString() will never return exponentiation in its result
Q 94. How would you round a double value to certain decimal Precision and Scale?
Firstly let us understand the difference between Precision and Scale.
No one wants to loose the precision of the number as it will change the value by large amount. If you still want
to loose the precision simply divide the number by 10 to the power precision.
There are multiple ways in Java to round the double value to certain scale, as mentioned in the below example
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
The first method of rounding using BigDecimal should be preferred in most scenarios.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 98
Q 95. How great is the Idea of synchronizing the getter methods of a shared mutable
state? What if we don't?
Synchronization serves two major purposes in a multi-threaded scenario, one is atomicity of the operation and
second is the memory visibility of the changes made by one thread to all other threads (Brian Goetz article on
read and write barriers)1. In case of getters the changes made to the shared variable will get reflected to the
new thread if the code block is synchronized, otherwise dirty reads may happen and the thread may see the
stale state of the shared object.
So all the methods returning the mutable protected state of the shared object must be synchronized unless the
field returned is immutable, final or volatile.
Let's take example of a simple Counter class.
public class Counter {
private int c = 0;
public synchronized void increment() {
c++;
}
/**Getter Must be synchronized to see the guaranteed correct value*/
public synchronized int getValue() {
return c;
}
}
That's the reason that get() method of vector class is synchronized & must be synchronized.
References
https://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
https://www.infoq.com/articles/memory_barriers_jvm_concurrency
http://www.oracle.com/technetwork/articles/java/goetz-qa-137779.html
The answer is NO. If we make the keys mutable then the hashcode() of the key will no more be consistent over
time which will cause lookup failure for that object from the data structure. Let's analyze this example.
public void testMutableKey() {
Map<MutableKey, Object> testMap = new HashMap<>();
MutableKey mutableKey = new MutableKey();
mutableKey.setName("TestName");
testMap.put(mutableKey, new Object());
Object o = testMap.get(mutableKey);
System.out.println("before changing key = " + o);
mutableKey.setName("abc");<====Problematic Instruction
o = testMap.get(mutableKey);
System.out.println("after changing key = " + o);
1 http://www.ibm.com/developerworks/library/j-jtp08223/
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 99
Then we modify the key and try to get the object. HashMap's get() method again calculates the hashcode of
the Key, since the Key is changed in between, so suppose hashcode() comes out to be 33 this time. Now the
get() method goes to the bucket at address 33 and tries to retrieve the object, but it find nothing over there and
returns the null.
Never make changes to the hashmap's key, otherwise the associated object can not be fetched using get()
method. Though it will be accessible using other methods which iterate over the entire collection.
We should synchronize the code block doing any kind of iteration as stated by the Java Docs
Returns a synchronized (thread-safe) collection backed by the specified collection. In order to guarantee serial
access, it is critical that all access to the backing collection is accomplished through the returned collection.
It is imperative that the user manually synchronize on the returned collection when iterating over it:
Collection c = Collections.synchronizedCollection(myCollection);
...
synchronized (c) {
Iterator i = c.iterator(); // Must be in the synchronized block
while (i.hasNext())
foo(i.next());
}
Q 98. What are different type of Inner classes in Java? How to choose a type with
example?
An inner class is a class defined within another class, or even within an expression. They are mostly used to
simply our code by putting closely related classes together in one source file, instead creating class burst.
Event handlers are best examples of Inner Classes.
Notes
Question : Why do we need to declare a local variable final if inner class declare within a method needs
to use it?
Local variables always live on the stack, the moment method is over all local variables are gone. Inner class
objects might be on heap even after the method is over, so in that case it would not be able to access the local
variable, since they are gone. There is also a possibility that the variable could change before the inner class
accesses it. Making the local variable final prevents these scenarios.
Q 99. When should we need a static inner class rather than creating a top level class
in Java program?
A static Class interacts with the instance members of its outer class and other classes just like any top level
class. In fact, a static nested class is behaviorally a top level class that has been nested in another top level
class for packaging convenience.
If we take a example of LinkedList.Entry class, there is no need of it being a top level class as it is only used by
LinkedList. Otherwise it will cause class burst inside a package, moreover there are other static inner classes
by the same name as well like Map.Entry
And since these does need access to LinkedList/Map's internal so it makes sense to make them static inner
classes.
Examples
Iterator in most of the collection types are implemented as a inner class and Entry is implemented as static
inner class.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 101
Q 100. How does method parameter passing works in Java? Does it pass-by-refer-
ence or pass-by-value?
Method parameters are always pass-by-value [of-the-reference] in Java language irrespective of the type of
variable (primitive or objects). Lets consider the below scenario -
Here, when we call method swap(x, y) from main() method, then we pass the value of reference x and y
(i.e. Value stored at memory address S1 and S2) rather than entire object from heap. In swap method, two
new stack variables are created with name a and b, pointing to address H1 and H2 on heap. Now when you
reassign these method variables while doing swap, you are actually changing the memory location that a and
b are pointing to. Since these method variables are local to method swap, so variable x and y in main method
will not see any effect of swap. In nutshell, swap method can not change values pointed by variable x and y in
main method.
For a primitive variable, a copy of the bits representing the value is passed to the method. For example, if you
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 102
pass an int variable with the value of x, you're passing a copy of the bits representing x. The called method
then gets its own copy of the value, to do with it what it likes.
For Objects, a copy of the bits representing the reference to an object is passed to the method. The called
method then gets its own copy of the reference variable, to do with it what it likes. But because two identical
reference variables refer to the exact same object, if the called method modifies the object (by invoking
setter methods, for example), the caller will see that the object the caller's original variable refers to
has also been changed.
The bottom line on pass-by-value: the called method can't change the caller's variable, although for object
reference variables, the called method can change the object the variable referred to. What's the difference
between changing the variable and changing the object?
That's the reason we can never write a method in Java which can swap two Integers or even mutable objects.
Above implementation will change the values referenced by variable x and y, but it will not reflect in the
method that calls swap(Integer, Integer). Thus swap will not work as intended.
The only way to have this possible was using some kind of setter on Integer class which could have modified
the underlying value. But Java declares all Wrapper classes as Immutable for thread-safety perspective, thus
there is no way to swap Integers in Java.
Create a mutable wrapper class that holds these Integer values, then pass this mutable Wrapper to the swap
method. Swap method shall set (not reassign) value on these wrapper objects while doing swap. Correct
working Java Code is shown below -
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 103
swap(first, second);
System.out.println(first.value);
System.out.println(second.value);
}
first.value = second.value;
second.value = tmp;
}
}
Hashcode is used to provide best case O(1) time complexity for searching the stored element.
TreeMap, TreeSet uses Comparator/Comparable for comparing the elements against each other, so these data
structures do not require hashcode() method. The best case time complexity offered by these datastructures
for lookup operation is logarithmic O (log n) compared to O (1) offered by hashing datastructures.
• Problem sharing a mutable static variable in multi-threaded environment. It's very tough to write & maintain
a thread safe code with Mutable non-private static fields.
• Problem in Single Threaded design because we have to be very careful while updating static variable,
since the next bit of code might expect some other state for the same.
• Code that relies on static objects can’t be easily unit tested, and statics can’t be easily mocked and hence
does not promote TDD.
- If you are using static keyword without final for declaring a fields then you should reconsider your design,
since the mutable static fields can be just dangerous !!
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 104
Iterable Map
Collection SortedMap
SortedSet Deque
NavigableSet
Java Collections Framework Overview
There is a separate utility class named Collections which provides various static factory methods for playing
with collections (Algorithm Part)
There are multitude Implementations provided for the above mentioned interfaces, few implementations
implements more than one such Interface.
Set - A collection that does not allow duplicate elements (models mathematical set abstraction) and represents
entities such as courses making up of student's schedule, ISBN number of books, Social security Number,
PAN number, processes running on a machine, etc
List - A collection that maintains order of its elements. Lists can contain duplicate elements. ListIterator
provides precise control over where to add the new item to the collection.
Queue - Queue is a First In First Out data structure which maintains order of its original elements. Most List
implementations like LinkedList implements Queue interface as well.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 105
Q 105. What is Immutable Class. Why would you choose it? How would you make a
class immutable?
Why do we need it
Immutable objects are inherently thread-safe, thus help writing multi-threading code without much worries.
Immutable questions are meant for multi-threading program. If someone is talking bout immutability then
indirectly he is talking about multi-threaded context. Immutable classes are easy to understand, as they
possess a single state, which is controlled by their constructor. Immutable objects are good candidate for hash
keys because their hashcode can be cached and reused for better performance.
Q 106. Discuss Exception class hierarchy in Java. When should we extend our
custom exception from RuntimeException or Exception?
Throwable sits at the top of Java Exception Class hierarchy. Exception and Error are the two direct
subclasses that extends Throwable.
Checked Exceptions Represents exceptional scenario which if occurred, must dealt with in some way.
example is IOException, FileNotFoundException. We need to declare these exceptions along with the code
dealing with such scenarios. Custom checked exceptions can be created by extending your class from java.
lang.Exception Class.
Unchecked/Runtime Exceptions Represents an error in our program's logic which can not be reasonably
recovered from at run time, for example NullPointerException, ArrayIndexOutOfBoundsException. We do
not need to declare/catch such exception in the method signature because these are not expected by any
programmer. Custom unchecked exceptions can be created by extending from RuntimeException
Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to
catch. A custom error can be created by extending our class from Throwable.
Q 107. How does an ArrayList expands itself when its maximum capacity is reached?
When the internal array of an ArrayList becomes full, then new array with double the capacity is created
efficiently by the ArrayList using the following method.
If it needs to shift the elements in order to add something over the existing index, then it displaces the elements using
following System method -
If we know in advance the capacity requirements for the ArrayList object, then we should always create the
ArrayList with that capacity to reduce the amount of incremental reallocation effort.
Also, as of Java 8, when an Array reaches its capacity (capacity is not size but length of internal array, for
arraylist), its size is increased 1.5 times compared to earlier capacity. This is excerpt from Java Source code -
which is 1 + 1/2 = 1.5 times the older capacity. Please note that right shifting an integer by 1 is equivalent to
division by 2.
Pool generally resides in PermGen space as of JDK 1.6 (not much documentation is found on this)
The pooling concept has been made possible because the String objects are Immutable in Java.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 108
Instance Lock
When we enter a non-static synchronized code then JVM acquires instance level lock on the Object whose
method we are executing. Instance lock can also we acquired using the following syntax with block level
synchronization.
Notes about instance locking
• Lock is mutually exclusive, which means that only one thread can acquire it and other threads have to wait
for their turn until first thread releases it.
• Each Java object has just one lock (or monitor)
• Non-synchronized methods (& a single static synchronized method) can be executed in parallel with a
single synchronized method.
• If a thread goes to sleep then it holds any lock it has.
• A single thread can acquire multiple lock on multiple objects.
• Lock is Reentrant, meaning that same thread can acquire the same lock multiple times (231 times)
synchronized(MyClass.class){
…
}
Example
Lets take this sample example to understand class and instance level Lock.
1. One Thread can call reference.classMethod() and other thread can call reference.instanceMethod() in
parallel because class level and instance level locks do not interfare.
2. But both the threads can't call the same instanceMethod() or classMethod() in parallel, because of the
Mutual Exclusiveness of the Instance Lock and Class Lock.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 109
Question : How will you Produce a Race Condition in your Java Program?
Answer
The following Class's object, if called from two different threads, can produce a Race Condition. When two
thread will try to add value to the single shared object, chances are there that race condition will occur and as a
result, the outcome of add method will be unpredictable.
Q 112. What is float-int implicit conversion while doing calculation on mixed data type
in Java?
If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point
operation, even if the other is integral.
int i1 = 5;
float f = 0.5f;
int i2 = 2;
System.out.println(i1 * f); // Result will be a float
System.out.println(i1 / i2); // Result will be an integer
System.out.println(((float) i1) / i2); // Result will be a float
Result
2.5
2
2.5
Primitive Casting
Casting lets you convert primitive values from one type to another. Casts can be explicit (narrowing
conversions) or implicit (widening the conversions).
Compiler does implicit conversion when you try to put smaller item into bigger bucket but not the other way.
By default all literal integers are implicitly interpreted as int by the compiler. for example,
int x = 27; //Literal assignment
Q 113. Discuss Comparable and Comparator? Which one should be used in a given
scenario?
Comparable and Comparator both are used for allowing sorting a collection of objects.
Comparable should be used to define the natural ordering behavior of an Object. Normally a class implements
this interface to define the natural ordering behavior of its objects. For example, java.lang.String implements
Comparable interface to provide natural order to Strings (Compares two strings lexicographically)
Comparator should be used to provide an external controllable ordering behavior which can override the
default ordering behavior (natural ordering) and when we might require different type of ordering behavior
for the same Object. Comparator is implemented like an Adaptor Design Pattern where a separate class is
dedicated for providing the comparison behavior.
Q 114. How would you sort a collection of data based on two properties of an entity in
Java, analogical to SQL's Order by firstField, SecondField desc?
Sorting based on multiple Object properties is easily achievable in Java Collections Framework. We just need
to redesign our Comparator to accommodate for multiple fields. Let's see how can we achieve that .
Lets assume we want to sort Person objects based on Age, and then Name (when two person has same age).
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Sorting {
static class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {return "name=" + name + ",age=" + age + '}';}
}
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Second", 26));
java.util.Collections.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
if (o1.age == o2.age) {
return o1.name.compareToIgnoreCase(o2.name);
} else if (o1.age < o2.age) {
return -1;
}
return 1;
}
});
System.out.println(java.util.Arrays.toString(persons.toArray()));
}
}
In the above code snippet, we can see the implementation for Person Comparator (code highlighted in red),
whenever age of two persons is equal, then return the result based on Name comparison. That's quite easy?
Achieving the same result in Java 8 is compact and easy, as seen below
persons.sort(
Comparator.comparing(Person::getLastName)
.thenComparing(Person::getFirstName)
.thenComparing(
Person::getEmailAddress,
Comparator.nullsLast(CASE_INSENSITIVE_ORDER)));
That's the magic of Java 8, its compact with Lambda expressions and functional interfaces.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 112
Q 115. How would you convert time from One Time Zone to another in Java?
java.util.Date class is not TimeZone aware, as it does not store any time zone specific information in its object.
This is clearly mentioned in the Java Docs for Date Class -
In Date, A milliseconds value represents the number of milliseconds that have passed since January 1, 1970
00:00:00.000 GMT.
The internal representation of the time inside Date object remains same for a given time, when we print the
date object using System.out.println(date) method then date.toString() method is invoked which prints the date
in local TimeZone of the JVM.
Thus a given time in milliseconds can be represented in different TimeZone using different TimeZone specific
Date formatters.
Notes
Always prefer to user Calendar API over Date due to various benefits of Calendar Class - Calendar handles
TimeZone information and it correctly measures the duration of a year in milliseconds keeping into account the
leap years.
instance.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date2 = instance.getTime();
System.out.println("date2 = " + date2);
Answer
Both the System.out will print the same date value because Date class object is always printed in local
TimeZone and changing the TimeZone on Calendar class does not alter the underlying milliseconds value from
the epoch time (Since January 1, 1970 00:00:00.000 GMT).
Question: How will you write a method to add weekdays to a given date?
Answer - The following method can add given weekdays to a given Date.
public Date addBusinessDays(Date date, int numberOfDays) {
int count = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
while (count < numberOfDays) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || calendar.get(Calendar.DAY_OF_WEEK) ==
Calendar.SATURDAY)
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 113
count++;
}
return calendar.getTime();
}
Q 116. Will WeakHashMap's entry be collected if the value contains the only strong
reference to the key?
public class Key {};
public class Value{
final public Key key;
public Value (Key key){
this.key = key;
}
}
Even if we specify a custom initial capacity, it will round it to the nearest power of two using the following code.
int capacity = 1;
while(capacity < intialCapacity){
capacity = capacity << 1;
}
Q 118. Can we traverse the list and remove its elements in the same iteration loop?
Yes, that is feasible, provided
1. No other thread is modifying the collection at that traversal time (it should be single threaded model)
2. Iterator is used to traverse and to remove the elements from within that loop
Q 119. Do I need to override object's equals() and hashcode() method for its use in a
TreeMap?
Only hashing data structures require hashcode() method for retrieving data in Big O(1) time complexity.
TreeMap, on the other hand uses Comparator/Comparable for sorting/ranking its elements, equals() is only
used to search an element inside the collection using contains(object o). Its always a good practice to keep
equals() method in sync with the Comparator to have consistency in your code.
HashMap, hashtable, ConcurrentHashMap, LinkedHashMap are few of the hashing data structures that are
dependent on both hashcode() and equals() method.
T item = queue.remove();
notify();
return item;
}
}
If we really want a single lock for the entire collection, then we should really prefer synchronized hashmap
instead of ConcurrentHashMap.
Q 122. How will you implement a Blocking Queue using Lock and Condition Interface
provided in JDK?
Lock is analogical to synchronized keyword and Condition is similar to wait/notify. Here is the implementation
for BlockingQueue that uses Lock and Condition.
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Q 123. How would you cancel a method execution after time-out expires using Java
Future?
This can be easily achieved using Future utility class provided by concurrent package in JDK 1.5
Future represents the life cycle of a task and calling cancel() on future attempts to cancel the task execution,
if not already in completed state. Thread can even be interrupted if we pass true as a parameter to cancel()
method. And this interrupt can be checked using Thread.interrupted() method inside the running task.
JAVA Source
import java.util.concurrent.*;
public void timedRun(Runnable runnable, long timeout, TimeUnit unit) throws InterruptedException,
ExecutionException {
Future<?> task = executorService.submit(runnable);
try {
task.get(timeout, unit);
} catch (TimeoutException e) {
System.err.println("Timeout occurred.");
} finally {
task.cancel(true);
}
}
timedExecution.timedRun(new Runnable() {
@Override
public void run() {
while (!Thread.interrupted()) {
System.out.println("Test me..");
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 117
}
}
}, 100, TimeUnit.MICROSECONDS);
timedExecution.stop();
}
}
This task will be cancelled after 100 microseconds. Do not forget to call task.cancel(true), otherwise the thread
will continue executing the task in background.
Q 124. Java already had Future interface, then why did they provide Completable
Future class in Java 8?
There is an inherent problem with the existing Future class in Java 1.5, lets see what's wrong with it !
The java.util.concurrent library provides a Future<T> interface to denote a value of type T that will be available
at some point in the future.
Let's say our requirement is to chain below two asynchronous operations in single method
1. Read the web page
2. Get links from the web page
We will analyze if it is feasible to chain these two operations using existing Future available in Java.
Let's assume there are two methods corresponding to above two operations
public void Future<String> readPage(URL url) {...definition...}
public static List<URL> getLinks(String page) {...definition...}
How can we chain the above two method to get the links from web page? Unfortunately, there is only one way.
First, call the get method on the future to get its value when it becomes available. Then, process the result:
public void getLinks(String url) {
Future<String> contents = readPage(url);
String page = contents.get();
List<URL> links = Parser.getLinks(page);
}
But the call to content.get() is a blocking call. We are really no better off than with a method public String
readPage(URL url) that blocks until the result is available. There was no easy way of saying: “When the result
becomes available, here is how to process it.” This is the crucial feature that the new CompletableFuture<T>
class provides.
Unlike a plain Future, a CompleteableFuture has a method thenApply to which you can pass the post-
processing function.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 118
JVM provides intrinsic synchronization through monitor locks. Each object in Java owns a monitor on which
the threads can be synchronized. JDK 1.5 introduced concept of explicit synchronization using Lock1 and
Condition classes which offers advanced features over intrinsic synchronization.
1 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 119
Q 126. What are Stamped Locks? How they are useful in optimistic scenario where
thread contention is rare?
StampedLock Class has been introduced in Java 8 for Optimistic Locking scenario, it provides three modes for
controlling read/write access. The state of a StampedLock consists of a version and mode.
1. writeLock() possibly blocks waiting for exclusive access, returning a stamp that can be used in method
unlockWrite(long) to release the lock.
2. readLock() possibly blocks waiting for non-exclusive access, returning a stamp that can be used in method
unlockRead(long) to release the lock.
3. tryOptimisticRead() returns a non-zero stamp only if the lock is not currently held in write mode. Method
validate(long) returns true if the lock has not been acquired in write mode since obtaining a given stamp.
This mode can be thought of as an extremely weak version of a read-lock, that can be broken by a writer
at any time. The use of optimistic mode for short read-only code segments often reduces contention and
improves throughput.
Example Usage
We should use StampedLock for writing thread-safe classes that have minimum thread contention, as
StampedLock highly relies on the Optimistic Locking and thus is very fragile to concurrent updates.
• We can improve the Java Vector Class's get method by using lighter weight optimistic lock
• Implementing Bank Account with StampedLock (bankAccounts have minimum thread contention, as
normally single person uses the account at a given time)
import java.util.concurrent.locks.StampedLock;
Q 127. How will you find out first non-repeating character from a string? For example,
String input = "aaabbbeggh", answer should be 'e'
We can find the first non-repeating char from a string using the following algorithm in Big O(n) Time
First Pass
We can maintain a counting array for all possible alphabet values (ASCII code 0-128) and keep on counting
the position of array based on Ascii value of character. This will be Big O(n) Time Complexity Task where n is
number of letters in the string.
Second Pass
Iterate through the counting array and find the first index position where value is exactly 1, and then break.
That will give us the ascii code of character that is non repeating.
Java Code
String str = "zzzzzbbbccccddehhhhiii";
int[] countingArray = new int[128];
str.chars().forEach(value -> countingArray[value]++);
int nonRepeatingCharAsInt = 0;
for (int i = 0; i < countingArray.length; i++) {
if (countingArray[i] == 1) {
nonRepeatingCharAsInt = i;
break;
}
}
System.out.println("character = " + Character.valueOf((char) nonRepeatingCharAsInt));
There is a second method using hashmap to store the count, using Java 8
import java.util.LinkedHashMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static java.util.function.Function.identity;
“Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially
executed by another thread. A Runnable, however, does not return a result and cannot throw a checked
exception.”
public interface Callable<V> {
V call() throws Exception;
}
In order to convert Runnable to Callable use the following utility method provided by Executors class
Callable callable = Executors.callable(Runnable task);
Callable, however must be executed using a ExecutorService instead of Thread as shown below.
result = exec.submit(aCallable).get();
Submitting a callable to ExecutorService returns Future Object which represents the lifecycle of a task and
provides methods to check if the task has been completed or cancelled, retrieve the results and cancel the
task.
Future Interface looks like this -
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
So eventually, we can cancel a future, get its status or synchronously wait for its completion by invoking
blocking get() method.
Q 129. What will happen when an exception occurs from within a synchronized code
block? Will lock be retained or released?
When an exception occurs from within a synchronized code block, then JVM smartly releases all the locks
acquired by the current thread and will start unwinding the execution stack, till the exception is handled using
catch block, otherwise killing the thread.
But the same does not happen when we write explicit locking code using Lock interface. In that case we need
to release the lock manually in the finally block.
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 123
2. wait() can only be called from synchronized context otherwise it will throw IllegalMonitorStateException,
while sleep can be called from any code block.
3. wait() is called on an Object while sleep is called on a Thread
4. waiting thread can be awaken by calling notify()/notifyAll() methods while sleeping thread can't be awaken1
(though can be interrupted)
5. Incase of sleep() Thread immediately goes to Runnable state after waking up while in case of wait(),
waiting thread first fights back for the lock and then go to Runnable state.
6. Major difference between yield and sleep in Java is that yield() method pauses the currently executing
thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If
there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue
its execution.
In Layman's Terms
sleep(n) - Thread is done with its time slot, and please don’t give it another one for at least n milliseconds. The
OS doesn’t even try to schedule the sleeping thread until requested time has passed.
yield() - Thread is done with its time slot, but it still has work to do. The OS is free to immediately give the
thread another time slot, or to give some other thread or process the CPU the yielding thread just gave up.
wait() - Thread is done with its time slot, Don’t give it another time slot until someone calls notify(). As with
sleep(), the OS won’t even try to schedule your task unless someone calls notify() or one of a few other
wakeup scenarios occurs (spurious wakeup).
Naming Convention
E - element
K - key
N - number
T - type
V - value
S,U,V etc - 2nd, 3rd & 4th type
Here List<String> is not a sub type of List<Object>, hence as per inheritance rule, this is not allowed.
Q 133. What are Upper and Lower bounds in Generics? Where to choose one?
Upper and Lower bounded wildcard are used in Generics to relax the restriction on a variable.
Wildcard Guidelines
•An "in" variable is defined with an upper bounded wildcard, using the extends keyword.
•An "out" variable is defined with a lower bounded wildcard, using the super keyword.
•In the case where the "in" variable can be accessed using methods defined in the Object class, use an
unbounded wildcard.
•In the case where the code needs to access the variable as both an "in" and an "out" variable, do not use a
wildcard.
Multiple Bounds
A type parameter can have multiple bounds as described below.
<T extends B1 & B2 & B3>
1 http://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 126
"Fields declared final are initialized once, but never changed under normal circumstances. The detailed
semantics of final fields are somewhat different from those of normal fields. In particular, compilers have
a great deal of freedom to move reads of final fields across synchronization barriers and calls to arbitrary
or unknown methods. Correspondingly, compilers are allowed to keep the value of a final field cached in a
register and not reload it from memory in situations where a non-final field would have to be reloaded.
final fields also allow programmers to implement thread-safe immutable objects without synchronization.
A thread-safe immutable object is seen as immutable by all threads, even if a data race is used to pass
references to the immutable object between threads. This can provide safety guarantees against misuse of an
immutable class by incorrect or malicious code. final fields must be used correctly to provide a guarantee of
immutability.
An object is considered to be completely initialized when its constructor finishes. A thread that can only see
a reference to an object after that object has been completely initialized is guaranteed to see the correctly
initialized values for that object's final fields.
The usage model for final fields is a simple one: Set the final fields for an object in that object's constructor;
and do not write a reference to the object being constructed in a place where another thread can see it before
the object's constructor is finished. If this is followed, then when the object is seen by another thread, that
thread will always see the correctly constructed version of that object's final fields. It will also see versions of
any object or array referenced by those final fields that are at least as up-to-date as the final fields are."
Briefly speaking
Memory visibility of final fields are guaranteed by the Java Memory Model and hence are thread safe in
multi-threaded scenario. Thus we prefer to use Immutable objects in the concurrent application to avoid any
memory visibility related problems.
Notes
Question
We have a legacy application in which a thread reads a config file and starts creating beans. It does so
by first creating the object and then setting the required properties on them. All this happens without any
synchronization because everything is happening serially. Once the objects are created, other threads pick
those objects and start processing. But somehow, we got the problem that one of the client thread is not seeing
the Correct Value for a bean.
Analysis
This could be a typical memory visibility issue in a multi-threaded environment. A properly constructed bean
would have to make its fields final in absence of synchronization, otherwise other threads may see the default
values for its fields. Thus moving the bean initialization code (setters) into constructor and making the fields
final should solve this problem. Otherwise we might need to synchronize the access to this particular bean -
read articles about proper publishing of an object in multi-threaded environment.2
A Properly Constructed Object in Multi-threaded scenario3
1 http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.7
2 http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html
3 http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html continued on 127
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 127
The values of final fields are populated inside an object's constructor. And if the object is constructed properly
(this reference does not escape during construction, don't start thread from within constructor, fields are final),
then the values assigned to the final fields in the constructor are guaranteed to be visible to all other threads
without the need of any synchronization. In addition, the visible values for any other object or array referenced
by those final fields will be at least as up-to-date as the final fields. Let's understand the following example,
import java.util.ArrayList;
class FinalFieldExample {
final int x;
final ArrayList<String> names;
int y;
static FinalFieldExample f;
public FinalFieldExample() {
x = 3;
y = 4;
names = new ArrayList<>();
names.add("First");
names.add("Second");
}
In this example, suppose Thread A calls FinalFieldExample.write() method and thus creates the object. Thread
B on other hand calls FinalFieldExample.reader() method and thus access's the object. Further suppose that
Thread B calls reader() once the writer() is finished creating the object. As per new Java Memory Model (JSR
133),
• Thread B executing the reader is guaranteed to see the value of 3 for field f.x - its a final primitive variable
• Thread B is guaranteed to see the value of "First" and "Second" for field f.names because names array is
referenced by a final field, and the value assigned to such object inside the constructor boundary will be
visible to other threads.
• There is not guarantee that Thread B will see "third" inside array f.names
• There is not guarantee that Thread B will see value of 4 for field f.y
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 128
Q 136. What do you think is the reason for String Class to be Immutable?
String class in Java is implemented as Immutable and there are various reasons for doing that,
• Immutability brings inherent thread-safety to the usage of String class in a concurrent program. So multiple
threads can work on the same String object without any data corruption. There is absolutely no need to
synchronize the code because of String objects.
• StringPooling is possible only because of immutability because the underlying contents of the String will
never change. This helps reducing the overall memory footprint of the application using lots of String
objects.
• Hash code for Immutable objects are calculated lazily on first usage and then cached for future reference.
This gives the benefit of performance when we use Immutable Key's in any hashing data structure.
String concatenation is implemented internally through the StringBuilder(as of JDK 1.5) class and its append
method. As of Java 5, Java will automatically convert the following string concatenation
Thus no temporary objects will be created for this type of concatenation. This JVM optimization may improved
further in upcoming releases.
Time Complexity : Big O (n) where n is the number of strings, 1000 in this case.
Q 138. Which data type would you choose for storing sensitive information, like pass-
words, and Why?
Normally, a character array should used for storing passwords. Here is the reason for choosing char array over
String -
• There is no way to clear a String Object from the memory, its up to GC to collect it.
• String objects are immutable and stored in a String Pool (may reside inside a PermGen space) which may
not at all be gc'd.
• Any person taking the heap dump can easily see the String literals.
• In case of an char array, we can always nullify it once we are done with the information, so not much
dependency on the GC, thus we are narrowing the time window for the life of sensitive data.
• Externalizable Interface extends the java.io.Serializable adding two methods - writeExternal &
readExternal.
• In case of Serializable, default serialization is used, while in case of Externalizable, the complete
serialization control goes to the application. Stating that means, we can not benefit from the default
serialization process when we choose Externalizable interface.
• We generally choose Externalizable when we want to save the output in our custom format which is other
than Java default serialization format like, csv, database, flat file, xml, etc
• readExternel() and writeExternal() methods are used to handle the serialization in case of Externilizabel
interface.
• In case of externalizable interface, we need to handle super type state, default values in transient variable
and static variables.
• Incase of Serialization, object is reconstructed using data read from ObjectInputStream but incase of
Externalizable, public no-arg constructor is used to reconstruct the object.
Notes
The code shown above is not thread safe, ideally NumberFormat should be created local to a thread using
ThreadLocal class, instead making it a static field of class.
continued on 131
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 131
Q 142. How would you write high performing IO code in Java? Can you write a
sample code for calculating checksum of a file in time efficient manner?
Intent of the interviewer is to know if you are familiar with Java's High Performance IO Channels.
Few of the times we wish the speed of C for doing some IO intensive task in our Java program. Calculation of
CRC is one of the task which requires an efficient IO implementation in order to give good performance which
is very close to the one we see in a similar C program (though not equivalent)
An InputStream in Java can be easily converted into an FileChannel using its getChannel() method. Let's
understand how to use channels using the following Checksum Calculation Program.
public static long calculateCRC(File filename) {
final int SIZE = 16 * 1024;
try (FileInputStream in = new FileInputStream(filename);) {
CRC32 crc = new CRC32();
FileChannel channel = in.getChannel();
int length = (int) channel.size();
MappedByteBuffer mb = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
byte[] bytes = new byte[SIZE];
int nGet;
while (mb.hasRemaining()) {
nGet = Math.min(mb.remaining(), SIZE);
mb.get(bytes, 0, nGet);
crc.update(bytes, 0, nGet);
}
return crc.getValue();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
throw new RuntimeException("unknown IO error occurred ");
}
If the input file is very large > 1 GB, then its better to calculate the CRC in iterations -
public static Long calculateCRC(File file) {
long t1 = System.currentTimeMillis();
long crcValue;
final int SIZE = 16 * 1024;
final int SIZE2 = 16 * 1024 * 1024 * 100;
try (FileInputStream in = new FileInputStream(file)) {
CRC32 crc = new CRC32();
FileChannel channel = in.getChannel();
long length = channel.size();
long iterations = length / SIZE2;
long reverseLength = 0l;
for (int i = 0; i <= iterations; i++) {
MappedByteBuffer mb = channel.map(FileChannel.MapMode.READ_ONLY, reverseLength, i == iterations?
length % SIZE2 : SIZE2);
reverseLength += SIZE2;
byte[] bytes = new byte[SIZE];
int nGet = 0;
while (mb.hasRemaining()) {
nGet = Math.min(mb.remaining(), SIZE);
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 133
mb.get(bytes, 0, nGet);
crc.update(bytes, 0, nGet);
}
}
crcValue = crc.getValue();
long t2 = System.currentTimeMillis();
System.out.println("Time for CRC = "+ (t2-t1));
return crcValue;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
throw new RuntimeException("unknown IO error occurred ");
}
Java's FileChannel provide much better performance compared to the InputStream, BufferedInputStream and
RandomAccessFile methods, because it utilizes the operating system specific optimization techniques under
the hood. On the similar basis, SHA256 or MD5 can be calculated efficiently using FileChannels
public static String calculateSHA256(File file) throws IOException, NoSuchAlgorithmException {
long t1 = System.currentTimeMillis();
final int SIZE = 32 * 1024;
final int SIZE2 = SIZE * 1024 * 100;
try (FileInputStream in = new FileInputStream(file)) {
MessageDigest md = MessageDigest.getInstance("SHA-256");
FileChannel channel = in.getChannel();
long length = channel.size();
long iterations = length / SIZE2;
long reverseLength = 0l;
for (int i = 0; i <= iterations; i++) {
MappedByteBuffer mb = channel.map(FileChannel.MapMode.READ_ONLY, reverseLength, i == iterations?
length % SIZE2 : SIZE2);
reverseLength += SIZE2;
byte[] bytes = new byte[SIZE];
int nGet;
while (mb.hasRemaining()) {
nGet = Math.min(mb.remaining(), SIZE);
mb.get(bytes, 0, nGet);
md.update(bytes, 0, nGet);
}
}
byte[] mdbytes = md.digest();
//convert the byte to hex format
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
long t2 = System.currentTimeMillis();
System.out.println(sb.toString());
System.out.println("Time for SHA2 = "+ (t2-t1));
return sb.toString();
}
}
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 134
Java Channels can be used in wide variety of IO tasks. For example, an efficient implementation of Http File
Download can be written using a FileChannel, as shown below.
con.setReadTimeout(10000);
con.setConnectTimeout(10000);
Above code snippet will transfer bytes from HttpURLConnection's Stream into FileChannel using
transferFrom() method of FileChannel.
" This method is potentially much more efficient than a simple loop that reads from the source channel and
writes to this channel. Many operating systems can transfer bytes directly from the source channel into
the filesystem cache without actually copying them."
Q 143. We have an Application and we want that only Single Instance should run for
that Application. If Application is already running then second instance should never
be started. How would you handle this in Java?
2.) Use a shared file lock using FileChannel and check if that temp file is already locked by some running
process or not. If yes then terminate the startup process for second instance. Let's see how we can achieve
this in the following code -
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
public class SingleInstanceLock {
private String appName;
private File lockFile;
private FileLock fileLock;
private FileChannel fileChannel;
public SingleInstanceLock(String appName) {
this.appName = appName;
}
public boolean isAppActive() {
try {
lockFile = new File(System.getProperty("user.home"), appName + ".tmp");
fileChannel = new RandomAccessFile(lockFile, "rw").getChannel();
try {
fileLock = fileChannel.tryLock();
} catch (OverlappingFileLockException e) {
System.out.println("Already Locked");
closeLock();
return true;
}
if (fileLock == null) {
System.out.println("Could not obtain lock");
closeLock();
return true;
}
lockFile.deleteOnExit();
return false;
} catch (Exception e) {
closeLock();
return true;
}
}
private void closeLock() {
try { fileLock.release();} catch (Exception e) {}
try { fileChannel.close();} catch (Exception e) {}
}
}
Chapter - Core Java Cracking Java Interviews (Java 8, Hibernate & Spring) 136
Q 144. Why do we need Reader Classes when we already have Streams Classes?
What are the benefit of using a Reader over a stream, in what scenario one should be
preferred.
InputStream and OutputStream operates at byte level (also called byte streams) while Reader and Writer
classes operates at the character level (char streams). Reader class is essentially a wrapper over InputStream
where it delegates the I/O related work to the byte stream and performs the translation of byte to character
using the given character encoding and character set. So Reader class provides a easy mechanism to the
developer to deal with the Character stream with an option to deal with different CharacterSets.
It is possible to convert byte stream to a character stream using InputStreamReader and OutputStreamWriter.
Convert Unicode Characters (from String object) to non-Unicode bytes using below code
static void writeOutput(String str) throws IOException {
FileOutputStream fos = new FileOutputStream("test.txt");
Writer out = new OutputStreamWriter(fos, "UTF8");
out.write(str);
out.close();
}
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 137
Chapter 3
Concurrency in Java
Q 145. What is Concurrency? How will you implement Concurrency in your Java
Programs?
Concurrency is the property of an software program to run several computations in parallel. Java provides
us with the multiple mechanisms to create Threads so as to utilize the multiple processor cores of a given
hardware in order to achieve high throughput.
Java provides various ready to use utilities for writing concurrent programs, which otherwise is difficult to
implement.
an example usage of TimeUnit and Lock interface could be to try obtaining lock for 50ms as shown in below
code snippet
Such a method call can never cause a deadlock scenario because it will try acquiring lock only for 50 ms only.
What is Synchronization?
Synchronization avoids thread interference and memory consistency errors by providing serialized access to
the shared state.
counter.
Counter.java
@NotThreadSafe
class Counter {
private int c = 0;
public void increment() {
c++;
}
public int value() {
return c;
}
}
This program will work absolutely fine in single threaded environment but will not behave correctly in multi-
threaded environment, because
1. increment() method will not be executed atomically so data race may corrupt the counter value.
2. value() method may not return the latest value of counter because of caching in processor's registers.
Concurrency vs Parallelism
Concurrency is when two tasks can start, run, and complete in overlapping time periods. It doesn't
necessarily mean they'll ever both be running at the same instant. Eg. multitasking on a single core
machine.
Parallelism is when tasks literally run at the same time, eg. on a multicore processor.
Q 146. There are two Threads A and B operating on a shared resource R, A needs to
inform B that some important changes has happened in R. What technique would you
use in Java to achieve this?
Object R's method wait(), notify() & notifyAll(), can be used for inter-thread
continued on 139
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 139
communication. This will allow all threads which hold lock over R, to communicate among them selves. You
can explore a typical Producer-Consumer problem to see how it works.
Q 147. What are different states of a Thread? What does those states tells us?
A thread in JVM can have 6 different states as defined in Thread.State enum. At any given time, thread must
be in any of these states.
NEW
This state is for a thread which has not yet started.
RUNNABLE
This state is for the currently running thread which is executing in java virtual machine, but it may be waiting for
the other resources from operating system such as processor.
BLOCKED
Thread state for a thread blocked waiting for a monitor lock. A thread in this state can be waiting for a monitor
lock to enter a synchronized block/method or reenter a synchronized method after calling Object.wait.
WAITING
A thread is waiting due to calling on one of the method -
Object.wait with no timeout
Thread.join with no timeout
LockSupport.park
A Thread in this state is waiting for another thread to perform a particular action. For example, a thread that
has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on
that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.
TIMED_WAITING
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to
calling one of the following methods with a specified positive waiting time -
Thread.sleep
Object.wait with timeout
Thread.join with timeout
LockSupport.parkNanos
LockSupport.parkUntil
TERMINATED
Thread state for a terminated thread. The thread has completed execution.
References
This content has been taken directly from the Java 7 Docs - Thread.State enum.
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 140
Q 148. Question: What do you understand by Java Memory Model? What is double-
checked locking? What is different about final variables in new JMM?
Interviewer's Intent - Interviewer wants to understand your capabilities to write robust concurrent code.
Java Memory Model1 defines the legal interaction of threads with the memory in a real computer system. In
a way, it describes what behaviors are legal in multi-threaded code. It determines when a Thread can reliably
see writes to variables made by other threads. It defines semantics for volatile, final & synchronized, that
makes guarantee of visibility of memory operations across the Threads.
Let's first discuss about Memory Barrier which are the base for our further discussions. There are two type of
memory barrier instructions in JMM - read barriers & write barrier.
A read barrier invalidates the local memory (cache, registers, etc) and then reads the contents from the main
memory, so that changes made by other threads becomes visible to the current Thread.
A write barrier flushes out the contents of the processor's local memory to the main memory, so that changes
made by the current Thread becomes visible to the other threads.
Thread A
-------------
data = new Data();
flag = true; <-- writing to volatile will flush data as well as flag to main memory
Thread B
-------------
if(flag==true){ <-- reading from volatile will perform read barrier for flag as well data.
use data; <--- data is guaranteed to visible even though it is not declared volatile because of the JMM
semantics of volatile flag.
}
JMM semantics for final fields & Initialization safety
JSR 133 (new JMM with JDK 1.5 onwards) provides a new guarantee of initialization safety - that as long as an
object is properly constructed (this reference does not escape during the construction), then all threads will see
the correct value for its final fields that were set in its constructor, regardless of whether or not synchronized is
used to publish the object from one thread to another. Further, any variable that can be reached through a final
field of a properly constructed object, such as fields of an object referenced by a final field, are also guaranteed
to be visible to the other threads. For example, if a final field contains reference to a ArrayList, in addition to the
correct value of the reference being visible to other thread, also the contents of that ArrayList at construction
time, would be visible to other threads without synchronization.
For all the final fields, when a constructor completes, all of the writes to final fields and to the variables
reachable through those final fields becomes frozen, and any thread that obtains a reference to that object
after the freeze is guaranteed to see the frozen values for all frozen fields. So it is a kind of happen-before
relationship between the write of a final field in the boundary of constructor and the initial load of a shared
reference to that object in another Thread.
NonThreadSafe Singleton (This will not work under current JMM), so never use it
public class Singleton
{
private Singleton() {}
private static Singleton instance_ = null; ==> A global static variable that will hold the state
because it didn't perform a read barrier, it could still see stale values of _instance's member fields.
• Since thread B is not executing inside a synchronized block, it may see these memory operations in
a different order than the one thread A executes. It could be the case that B sees these events in the
following order (and the compiler is also free to reorder the instructions like this): allocate memory, assign
reference to resource, call constructor. Suppose thread B comes along after the memory has been
allocated and the resource field is set, but before the constructor is called. It sees that resource is not null,
skips the synchronized block, and returns a reference to a partially constructed Resource! Needless to say,
the result is neither expected nor desired.
Fixed double-checked Locking using volatile in new JMM (multi-threaded singleton pattern JDK 1.5)
The following code makes the helper volatile so as to stop the instruction reordering. This code will work with
JDK 1.5 onwards only.
class Foo {
private volatile Helper helper = null;
public Helper getHelper() {
if (helper == null) {
synchronized(this) {
if (helper == null)
helper = new Helper();
}
}
return helper;
}
}
If Helper is an immutable object, such that all of the fields of Helper are final, then double-checked locking
will work without having to use volatile fields. The idea is that a reference to an immutable object (such as a
String or an Integer) should behave in much the same way as an int or float; reading and writing references to
immutable objects are atomic.
Alternatives to DCL2
Now a days JVM is much smarter and the relative expense of synchronized block over volatile is very less, so
it does not really make sense to use DCL for performance reasons.
The easiest way to avoid DCL is to avoid it. We can make the whole method synchronized instead of making
the code block synchronized.
Another option is to use eager initialization instead of lazy initialization by assigning at the creation time
Here is the example demonstrating eager initialization
class MySingleton {
public static Resource resource = new Resource();
}
}
public static Something getInstance() {
return LazyHolder.INSTANCE;
}
}
This code is guaranteed to be correct because of the initialization guarantees for static fields; if a field is set in
a static initializer, it is guaranteed to be made visible, correctly, to any thread that accesses that class.
The method increment() in the above code is not thread safe, because i++ require multiple cpu instruction
cycles to compute the summation. Data race condition may happen if the shared object is incremented from
multiple threads, simultaneously.
If you want, you can write your custom AbstractQueueSynchronizer to achieve the same, but that discussion
is out of scope for this writing.
Q 150. What happens when wait() & notify() method are called?
When wait() method is invoked from a synchronized context, the following things happen
• The calling thread gives up the lock.
• The calling thread gives up the CPU.
• The calling thread goes to the monitor's waiting pool.
ThreadPoolExecutor is an ExecutorService that uses a pool of thread (comprising of one or more threads) to
execute each submitted tasks. It provides many benefits over naive approach of creating a new thread for each
submitted task -
1. Usually improved performance when executing a large number of asynchronous tasks, due to sharing of
thread pool for consecutive tasks.
2. It provides a means to bound the resources as per configuration thus keeping your application predictable
at extreme input load. Both threads and Work Items can be bounded to maximum limit while creating a new
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 145
ThreadPoolExecutor instance.
3. Configurable rejection handler gets invoked when a particular task can not be executed due to some known
reason (Thread Pool shutting down, queue can not hold more work items, etc.)
New tasks submitted in method execute(Runnable) will be rejected when the Executor has been shut
down, and also when the Executor uses finite bounds for both maximum threads and work queue
capacity, and is saturated. In either case, the execute method invokes the RejectedExecutionHandler.
rejectedExecution(Runnable, ThreadPoolExecutor) method of its RejectedExecutionHandler. Four predefined
handler policies are provided:
1. In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException
upon rejection.
2. In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides
a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
3. In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
4. In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the
work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)
Here is the Java Code example that creates a new ThreadPoolExecutor with AbortPolicy()
int poolSize=2;
int maxPoolSize=5;
int queueSize=3;
long aliveTive=1000;
ArrayBlockingQueue<Runnable> queue= new ArrayBlockingQueue<Runnable>(queueSize);
ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(poolSize,maxPoolSize,aliveTive,
TimeUnit.MILLISECONDS,queue,new ThreadPoolExecutor.AbortPolicy());
Q 152. How will you write a custom ThreadPoolExecutor that can be paused and
resumed on demand? You can extend the existing ThreadPoolExecutor to add this new
behavior.
We can easily extend the Java provided ThreadPoolExecutor to add pause/resume behavior, as illustrated in
the below program.
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
super.beforeExecute(t, r);
pauseLock.lock();
try {
while (isPaused) unpaused.await();
} catch (InterruptedException ie) {
t.interrupt();
} finally {
pauseLock.unlock();
}
}
Q 153. How will you write your own custom thread pool executor from scratch?
Thread pool executor requires a Queue for holding tasks, and a collection of Worker Threads that will pick
up tasks from the work queue start running them. Let us try to write our own simple Thread Pool Executor
implementation.
package org.shunya.interview.example2;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
int i = 0;
for (Thread t : workerThreads) {
t = new Worker("Custom Pool Thread " + ++i);
t.start();
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 148
}
}
This is a very basic functionality for a Thread Pool Executor, more behavior can be added to it as and when
required.
A write to volatile variable not only flush changes of the volatile variable but all the non volatile variables
changed before write to volatile. Thus a simple flag of volatile type can serve the memory visibility guarantee of
all the other variables changed before. The following figure explain it in entirety.
Figure : Effects of volatile variables on time scale
Thread-1
------------
content = new Data();
ready = true;
Thread-2
------------
if(ready){
//process new content
int var = content.consume();
}
For the purposes of the Java programming language memory model, a single write to a non-volatile long or
double value is treated as two separate writes: one to each 32-bit half. This can result in a situation where a
thread sees the first 32 bits of a 64-bit value from one write, and the second 32 bits from another write.
Writes and reads of volatile long and double values are always atomic.
Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or
64-bit values.
It is safe to perform read-modify-write operations on a shared volatile variable as long as you ensure that the
volatile variable is only written from single thread.
volatile variables are liable to get into race conditions because atomicity is required to solve race condition
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 150
Q 156. What is a CAS? How does it help writing non-blocking scalable applications?
Tell something about Atomic Package provided by Java 1.6
This method (which varies in argument types across different classes) atomically sets a variable to the
updateValue if it currently holds the expectedValue, reporting true on success.
1 http://www.ibm.com/developerworks/java/library/j-jtp11234/
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 151
Q 157. There is a object state which is represented by two variables. How would
you write a high throughput non-blocking algorithm to update the state from multiple
threads?
Non-blocking algorithms provide better throughput at low thread contention compared to the locking
counterpart. This can only be achieved in Java using CAS1 (compare and swap) utilities provided in atomic
package. AtomicReference along with Immutable object can be used to write a non-blocking algorithm
maintaining a current state of Trade Volume.
There are key points to be noted while writing non-blocking algorithm2 are:
• Immutability of TradeVolume as in below example is must to ensure proper initialization at it's assignment
time. Immutability is achieved by making all fields final and providing constructor initialization.
• compareAndSet operation must be called repetitively in a while loop till the time it returns true.
The update method can be written concisely using the updateAndGet() method introduced in Java 8
"Atomically updates the current value with the results of applying the given function, returning the updated
value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to
contention among threads."
“Atomic classes are not general purpose replacements for java.lang.Integer and related classes. They do not
define methods such as hashCode and compareTo. (Because atomic variables are expected to be mutated,
they are poor choices for hash table keys.) Additionally, classes are provided only for those types that are
commonly useful in intended applications. For example, there is no atomic class for representing byte. In
those infrequent cases where you would like to do so, you can use an AtomicInteger to hold byte values, and
cast appropriately. You can also hold floats using Float.floatToIntBits and Float.intBitstoFloat conversions, and
doubles using Double.doubleToLongBits and Double.longBitsToDouble conversions.”
First method is to use AtomicReference to hold Double value as shown in below snippet
import java.util.concurrent.atomic.AtomicReference;
Second method is to use AtomicInteger for storing Float bit values as hinted by above Java docs
import java.util.concurrent.atomic.AtomicInteger;
import static java.lang.Float.*;
3 http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/package-summary.
html
continued on 153
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 153
public AtomicFloat() {
this(0f);
}
public AtomicFloat(float initialValue) {
bits = new AtomicInteger(floatToIntBits(initialValue));
}
public final boolean compareAndSet(float expect, float update) {
return bits.compareAndSet(floatToIntBits(expect),
floatToIntBits(update));
}
public final void set(float newValue) {
bits.set(floatToIntBits(newValue));
}
public final float get() {
return intBitsToFloat(bits.get());
}
Notes
Java 8 provides LongAdder and DoubleAdder to address atomic update problems for Long and Double. Java
Also provides LongAccumulator and DoubleAccumulator to accumulate values from different threads and then
consolidate them in final stage.
adder.accumulate(value);
//final thread
adder.doubleValue();
Q 159. How LongAdder and LongAccumulator are different from AtomicLong &
AtomicInteger?
When you have a very large number of threads accessing the same atomic values, performance suffers
because the optimistic updates require too many retries. Java 8 provides classes LongAdder and
LongAccumulator to solve this problem. A LongAdder is composed of multiple variables whose collective sum
is the current value. Multiple threads can update different summands, and new summands are automatically
provided when the number of threads increases. This is efficient in the common situation where the value of
the sum is not needed until after all work has been done. The performance improvement can be substantial.
Thus, If you anticipate high contention, you should simply use a LongAdder instead of an AtomicLong.
LongAdder Example
executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.SECONDS);
final long sum = adder.sum();
assert sum == numberOfThreads * numberOfIncrements;
}
In the above example, multiple threads (8 in number) are concurrently incrementing a LongAdder object.
LongAdder uses a clever tactics to reduce the thread contention during operations by multiple threads. It does
so by keeping an array of counters that can grow on demand. So there is at an expense of higher memory
consumption.
Q 160. Can we implement check & update method (similar to compare and swap)
using volatile alone?
No, this is not possible using volatile keyword alone. Volatile keyword can not guarantee atomicity of operation.
It's a lighter weight synchronization which can guarantee memory visibility only.
The only way to implement CAS is either using synchronized block (Lock Interface as well) or using java
provided hardware level CAS in it's atomic package i.e. using AtomicReference, AtomicInteger, etc
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 155
There are some generally usable features in Java SE 8 that are already implemented using the fork/join
framework.
• parallelSort() method introduced in java.util.Arrays leverage concurrency via the fork join framework. This
makes parallel sorting operation faster on multi-core machines compared to sequential sorting.
• Parallelism implemented in Stream.parallel() uses fork join framework under the hood.
Fork Join framework reduces the contention for the work queue by using work stealing technique. Each worker
thread has its own work queue, which is implemented using a double-ended queue (Deque, ArrayDeque,
LinkedBlockingDeque). When a task forks a new thread, it pushes it onto the head of its own Deque.
When a task executes a join operation with another task that has yet to complete, rather than sleeping until the
target task is complete (as Thread.join() does), it pops another task from the head of its Deque and executes
that. In case the thread's task Deque is empty, it tries to steal task from the tail of another thread's Deque.
There are at least two advantages of using the Deque instead of normal Queue in this case -
• Reduced Contention - Only worker thread ever accesses the head of its own Deque, there is never
contention for the head of the Deque. Similarly tail of the Deque is only ever accessed when a thread runs
out of work, there is rarely contention for the tail of any thread's Deque either. This reduction in contention
dramatically reduces the synchronization costs compared to a traditional thread-pool based frameworks.
• Reduced Stealing - LIFO ordering of the tasks means that the largest tasks sit at the tail of the Deque and
thus when another thread has to steal a task, it steals a large one that can be decomposed into smaller
ones, reducing the need to steal again in the near future.
Work Stealing thus produces reasonable load balancing with no central coordination and minimal
synchronization costs.
Few important points to consider before we start implementing Fork Join Pool for a given requirement:
• Choosing the optimum threshold value for sequential computation is very important, otherwise we may not
get the better results on multi-core hardware
• If the threshold is too small, then the overhead of task creation and management could become significant.
In this case machine memory may become bottleneck and counter any gain in the throughput.
• If the threshold is too large, then the program might not create enough tasks to fully take advantage of the
available cores in processor
• In case of doubt, we should perform a benchmark to see if there is actually gain in throughput due to Fork
Join Pool
• JDK documentation suggests, a task should perform more than 100 and less than 10000 basic
computational steps, and should avoid indefinite looping.
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 156
import java.util.concurrent.*;
Diagram on the next page explains the working of RecursiveTask execution in Fork Join Pool (using Divide and
Conquer approach).
Q 163. How will you track the largest value monitored by different threads in an non-
blocking fashion (using atomic operations)?
There is a shared long value which is updated simultaneously by multiple threads. Interviewer wants you to
track the largest value taking into consideration the thread-safety aspects.
We can use AtomicLong (from java.util.concurrent package) to achieve this behavior in a non-blocking fashion.
A naive approach could be to use AtomicLong.set(<long>), like shown in the following code:
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 157
The above code is prone to error due to race condition. Calculating the max value and setting the calculated
value into largest is not a atomic operation.
Correct approach using Java 7 would be to compute the new value and use compareAndSet in a loop:
Java 7 approach
public class LargestLongTracker {
private final AtomicLong largest = new AtomicLong();
Best approach
Java 8 introduced accumulateAndGet & updateAndGet that accepts a accumulator function (lambda) to
perform the action in non-blocking fashion.
In new Java 8 code, we shall always prefer this approach over other options.
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 158
split
1 2 3 4 5 6 7 8 9 10 11 12
split split
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
Final Result
r = f+g
Diagrammatic representation of Recursive Task that sums all elements of array using
Fork Join Pool
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 159
Fibonacci(int n) {
this.n = n;
}
When we invoke fork() on a RecursiveTask, a new sub-task is pushed to head of its own Deque. If the current
thread is working on its full capacity, another thread may steal this sub task and start executing it. Invoking
join() on the task, causes current thread to halt the current execution and pops the another task from the head
of its Deque, thus making Fork Join approach much more efficient.
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 160
Q 165. How will you increment each element of an Integer array, utilizing all the cores
of processor?
We can use Fork and Join Task to divide this problem into smaller subsets that can be executed in multiple cpu
cores, as shown below -
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
Q 166. You are writing a multi-threaded software piece for NSE for maintaining the
volume of Trades made by its individual brokers (icici direct, reliance ). It's highly con-
current scenario and we can not use lock based thread safety due to high demand of
throughput. How would handle such scenario?
private ConcurrentHashMap<String, BigDecimal> sumByAccount;
Since the multiple threads could be simultaneously adding the value to their respective broker, the designed
code should be thread safe. Un-Thread safe version looks like :
public void addToSum(String account, BigDecimal amount){
BigDecimal newSum = sumByAccount.get(account).add(amount);
sumByAccount.put(account, newSum);
}
Solution
CAS can be utilized for achieving the high throughput requirement of the underlying system in this case.
AtomicReference<BigDecimal> could be used to store the BigDecimal value atomically.
AtomicReference uses CAS to atomically compare and assign a single reference. In the above code the
compareAndSet(oldVal, oldVal.add(amount)) method checks if the AtomicReference == oldVal (by their
memory location instead of actual value), if true then it replaces the value of field stored in AtomicReference
with the oldVal.add(amount). All this comparison and swapping happens atomically by the JVM. Afterwards
invoking the newSum.get() will return the added amount.
For loop is required here because it is possible that multiple threads are trying to add to the same
AtomicReference and doing so just one thread succeeds and other fails. The failed threads must try again the
operation to make the addition to BigDecimal.
Please be noted that CAS is recommended for moderate Thread contention scenarios. Synchronized should
always be preferred for high contention code blocks. Or prefer to use LongAccumulator/ DoubleAccumulator if
you are using Java 8
Java 8 Can use DoubleAccumulator for the same purpose with much compact code (Preferred
Approach), addToSum(account, amount) method will look like below in Java 8
Q 167. Calculate the time spread for 10 threads - Suppose T1 started earliest and T5
finished last, then the difference between T5 and T1 will give time spread.
This is a typical thread synchronization problem which can be solved using various available techniques in
Java. We will discuss three main approaches to solve this problem - first one using a synchronized object,
second one using non-blocking CAS, third using existing synchronizer CountDownLatch. Algorithm for the both
is same - Two times will be recorded, first time for the thread which started earliest, and second time for the
thread which finished last. The difference of the two times will give us time window.
continued on 163
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 163
This version does not require the calling thread to obtain lock on the Object, thus it could be slightly faster.
public class TimeSpread {
final AtomicBoolean started = new AtomicBoolean(false);
final AtomicInteger stopCounter;
long startTime;
long stopTime;
public TimeSpread(int threads) {
stopCounter = new AtomicInteger(threads);
}
public void start() {
if (!started.get()) {
if (started.compareAndSet(false, true)) {
startTime = System.currentTimeMillis();
}
}
}
public void stop() {
if(stopCounter.getAndDecrement()==1){
stopTime = System.currentTimeMillis();
}
}
public long timeConsumed(){
return stopTime-startTime;
}
public static void main(String[] args) throws InterruptedException {
int threads = 300;
final TimeSpread timeSpread = new TimeSpread(threads);
Runnable t = new Runnable(){
public void run() {
timeSpread.start();
try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {}
timeSpread.stop();
}
};
List<Thread> list= new ArrayList<>(threads);
for(int i=0;i< threads;i++){
Thread thread = new Thread(t);
thread.start();
list.add(thread);
}
for (Thread thread : list) {
thread.join();
}
System.out.println("time spread = " + timeSpread.timeConsumed());
}
public static long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
Notes
We can evaluate any of these three approaches for our requirement and pick one. But definitely, using
CountDownLatch seems a cleaner approach where the latch hides the boiler-plate code of the synchronization.
Q 169. There is a stream of words which contains Anagrams. How would you print
anagrams in a single bucket from that stream?
What is Anagram?
A word or phrase spelled by rearranging the letters of another word or phrase, using all the original letters
exactly once. For example, aabbaa is anagram of aaaabb, ababab and bbaaaa
Arrays.sort(a1);
Arrays.sort(a2);
if (Arrays.toString(a1).equals(Arrays.toString(a2))){
return true;
}
return false;
}
There is O(n) Time Complexity algorithm for finding the anagram as well, you just need to iterate through the
first word and count the number of instances of each letter. Then iterate over the second word and do the
same. Finally make sure that the letter frequency matches for both the words.
Please note that the above implementation will not work for Unicode Characters, you may need to increase the
size of letterCount array in order to support characters outside ASCII range.
Unfortunately, this efficient algorithm can not be used with this requirement due to bucketing logic.
1. Create a hashmap with string as key and list<string> to hold all anagrams of a given key string.
2. For each word in the input stream, create a key by sorting the word and append this word to that list whose
key is the sorted word. For example [aakk -> akka, akak] If it does not exist then create a new list with the
sorted word as key in map.
3. Iterate through hashmap keys and print corresponding values.
Source Code
import java.util.*;
Time Complexity
If we ignore the time consumed by sorting an individual string then we can say that the above approach takes
Big O(n) time complexity. Otherwise the actual runtime complexity would be
This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly
outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to
preclude interference among concurrent threads.
The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator
was created. This array never changes during the lifetime of the iterator, so interference is impossible and
the iterator is guaranteed not to throw ConcurrentModificationException. The iterator will not reflect additions,
removals, or changes to the list since the iterator was created. Element-changing operations on iterators
themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.
Q 171. There are M number of Threads who work on N number of shared synchro-
nized resources. How would you make sure that deadlock does not happen?
If a single thread uses more than one protected shared resource, then we should make sure that we acquire
shared resources in particular order and release them in reverse order, otherwise we might end up into a
deadlock scenario.
Q 172. Are there concurrent version for TreeMap and TreeSet in Java Collections
Framework?
Java Collection Framework have ConcurrentSkipListMap and ConcurrentSkipListSet which are concurrent
replacement for TreeMap and TreeSet. These classes implement SortedMap and SortedSet interface
respectively. So if our application demands fair concurrency then instead of wrapping TreeMap and TreeSet
inside synchronized collection, we can prefer these concurrent utilities. These also implement NavigableMap
and NavigableSet interface with methods like lowerKey, floorKey, ceilingKey, higherKey, headMap and tailMap.
Time Complexity
Average time complexity is log(n) for the containsKey, get, put, remove ad the variant operations of the
ConcurrentSkipListMap
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 168
Q 173. Is it safe to iterate over an ArrayList and remove its elements at the same
time? When do we get ConcurrentModificationException & hidden Iterator?
Iterator returned by the ArrayList (and many other collection types) is fail-fast. If the list is structurally modified
at anytime after the iterator is created, in any way except through the Iterator's own remove() method, the
iterator will throw ConcurrentModificationException and thus fails quickly and cleanly, rather than risking
arbitrary, non-deterministic behavior at an undetermined time in the future.
Structural Modification
“A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the
backing array; merely changing the values associated with a key that an instance already contains is not a
structural modification.” - Java Docs
Further, the structural modification could happen either from single thread or from multiple threads. The
behavior of ArrayList would be different in both the cases as mentioned below.
Multi-Threading Scenario
ArrayList implementation is not thread-safe because it provides no synchronization mechanism for protecting
the shared state of its fields. If multiple threads access an ArrayList instance concurrently, and at least one of
the threads modifies the list structurally, it must be synchronized externally. This is typically accomplished by
synchronizing on some object that naturally encapsulates the list.
If no such object exists, the list should be "wrapped" using the Collections.synchronizedList() method. This is
best done at creation time, to prevent accidental unsynchronized access to the list :
Hidden Iterators
There are certain ArrayList methods which uses Iterators in a hidden form the API user. size() and toString()
are few of them. So care must be taken to call these methods from synchronized block in case of multi-
threaded scenario.
Java 8 provides method to conditionally remove items from stream using a filter
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 169
Though lambda makes removal quite compact, but the operation is not thread-safe and must not be used in
multi-threaded environment without explicit synchronization in place.
Q 174. What is ThreadLocal class, how does it help writing multi-threading code? any
usage with example?
ThreadLocal class provides a simple mechanism for thread safety by creating only one object instance per
thread. These variables differ from their normal counterparts in that each thread that accesses one (via its get
or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically
private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).
Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and
the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are
subject to garbage collection (unless other references to these copies exist)
ThreadLocal is used to achieve thread-safety in our multi-threaded code by creating a copy local to a thread
and thus no more sharing of state.
When get() method is invoked the first time, ThreadLocal calls initialValue() and returns the newly created
Object, the same Object is returned on subsequent invocations by the same thread untill we clear the Object.
threadLocalJPASession.get().close();
threadLocalJPASession.remove();
}
}
• Using Calendar class in multi-threading environment : Calendar.getInstance() is not safe from multi-
threading perspective and a copy of it could be created per thread and stored in ThreadLocal.
• Random Number Generator, ByteBuffers, XML parsers can utilize ThreadLocal for optimization purpose.
Notes
ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread
(e.g., a user ID or Transaction ID).
Q 175. How would you implement your own Transaction Handler in Core Java, using
the EntityManager created in last question?
Sometimes we do not want to use Spring Transaction API's and want to write our own (though we should
never do that unless we are very good at it). In the last question we discussed how we can write a ThreadLocal
EntityManager class. Now we will leverage the same class for writing our basic reusable transaction handler.
@Override
public<T> T run(UnitOfWork<T> unitOfWork) {
final EntityManager em = threadLocalSession.getUnderlyingEntityManager();
EntityTransaction tx = null;
try {
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 171
tx = em.getTransaction();
tx.begin();
T result = unitOfWork.run();
tx.commit();
return result;
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
threadLocalSession.clear();
}
}
}
Now any client code who wants to run a database specific code inside a transaction can create instance of
JPATransatomatic class, set appropriate ThreadLocalEntityManager and use it, as shown below
Q 176. What is AtomicInteger class and how is it different than using volatile or syn-
chronized in a concurrent environment?
Read & write to volatile variables have same memory semantics as that of acquiring and releasing a monitor
using synchronized code block. So the visibility of volatile field is guaranteed by the JMM.
AtomicInteger class stores its value field in a volatile variable, thus it is a decorator over the traditional volatile
variable, but it provides unique non-blocking mechanism for updating the value after requiring the hardware
level support for CAS (compare and set).
Under low to moderate thread contention, atomic updates provides higher throughput compared to
synchronized blocking increment operation.
You can see that no lock is acquired to increment the value, rather CAS is used inside infinite loop to update
the new value.
Chapter - Concurrency Cracking Java Interviews (Java 8, Hibernate & Spring) 172
Q 177. You are writing a server application which converts microsoft word documents
into pdf format. Under the hood you are launching a binary executable which does the
actual conversion of document. How would you restrict the parallel launch of such
binaries to 5 in Java, so as to limit the total load on the server.
This is a typical problem of controlling the parallel access to the shared scarce resource so as to avoid the
thread starvation.
JDK 1.5 provides a class specifically designed to address this kind of problem - Semaphore
Semaphore
Counting semaphores are used to control the number of activities that can access a certain resource or
perform a given action at the same time, and it could be used for
import java.io.File;
import java.util.concurrent.Semaphore;
}
});
Notes
Before Java 1.5, we had to write the semaphore functionality from scratch using synchronization (along with
wait and notify for inter thread communication)
Semaphores can be used to convert a standard Java Collection into Bounded Collection after which the
collection would hold only certain number of elements. Once the allowed elements are In, the thread has to
wait till some other thread removes from that collection.
BoundedHashSet Example
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Semaphore;
sem.release();
return wasRemoved;
}
}
Chapter 4
Algorithms & Data Structures
Q 179. Given a collection of 1 million integers ranging from 1 to 9, how would you
sort them in O(n) time?
This is a typical Integer Sorting problem with a constraint that the number range to sort is very limited in spite 1
million total entries. Integer Sorting with limited range is achieved efficiently with Bucket Sorting.
Algorithm
Create a array of size 9 and at each index store the occurrence count of the respective integers. Doing this will
achieve this sorting with time complexity of Big O(n) and even the memory requirements are minimized. In Order
to print the output just traverse the above created array.
Source Class
public class BucketSort {
public int[] sort(int[] array, int min, int max) {
int range = max - min +1;
int[] result = new int[range];
for (int i : array) {
result[i]++;
}
return result;
}
}
Test Class
public class BucketSortTest {
@Test
public void testBucketSortFor1To9() {
int[] array = {2, 1, 5, 1, 2, 3, 4, 3, 5, 6, 7, 8, 5, 6, 7, 0};
int[] sort = new BucketSort().sort(array, 0, 8);
for (int i = 0; i < sort.length; i++) {
for(int j=0;j<sort[i];j++){
System.out.println(i);
}
}}}
Notes
Bloom Filter1 could help us achieve something similar.
Bucket sort, counting sort, radix sort, and van Emde Boas tree sorting all work best when the key size is small;
for large enough keys, they become slower than comparison sorting algorithms…
Integer Sorting Techniques : http://en.wikipedia.org/wiki/Integer_sorting#Algorithms_for_few_items
Sorting Algorithms : http://en.wikipedia.org/wiki/Sorting_algorithm
1 http://en.wikipedia.org/wiki/Bloom%5Ffilter
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 176
Q 180. Given 1 million trades objects, you need to write a method that searches if
the specified trade is contained in the collection or not. Which collection would you
choose for storing these 1 million trades and why?
HashSet is a good choice for storing this collection because it will offer Big O(1) time complexity. In order to
use HashSet we must override equals() and hashcode() method for the Trade Object. If that’s not possible then
we should created a Trade Wrapper class which overrides these methods.
Q 181. I have an Integer array where every number appears even number of time
except one. Find that number.
Approach
This problem can be solved by utilizing bitwise operators in O(1) space and O(n) time complexity.
XOR all the number together and the final result would the odd number.
Output:
singleOdd = 5
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 177
Q 182. how would you check if a number is even or odd using bit wise operator in
Java?
Least significant bit (rightmost) can be used to check if the number is even or odd.
For all Odd numbers, rightmost bit is always 1 in binary representation.
Notes
We prefer bitwise operator for checking even odd because the traditional way of checking even by n % 2 ==0
is compassionately expensive compared to bitwise & operator (Big O(1) time complexity)
From the figure, we can see that only 1 bit is set for all numbers which are exponent of 2.
Let's now write a method to check if only nth bit of a number is set to true.
The second condition (x & (x-1)) will return zero for Binary presentation of 2's exponent
a number which is exponent of two (provided the
number is positive). For example, in case of number 32,
00010000 (25)
& 00001111 (25-1)
--------------------------------
00000000 (0)
Thus through the above code, we are checking if the number is positive and is power of two.
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 178
Q 184. What is a PriorityQueue? How is it implemented in Java? What are its uses?
What is a PriorityQueue?
It is an abstract data type similar to queue but the elements are stored in a sorted order according to some
priority associated with them, and the element with the higher priority is served before the element with lower
priority. Priority is decided using the Comparator provided at the time of its construction. If no comparator is
provided, then the natural ordering of elements is used to prioritize them.
For example, if all elements are of type Integer and no comparator is provided, then the natural order is used
resulting in highest priority to the smallest Integer value.
Given the index of an element, element's children can be accessed in constant time using an random access
array. Children of the element at index i are at indexes (i << 1)+1 and (i << 1)+2. And the parent of an element
at index j is at (j-1) >>1
PriorityQueue Usages
• A network printer where multiple people submit print jobs at the same time, While one big print job is
executing, PriorityQueue could re-arrange other jobs so that the small print jobs (with very less number of
pages) execute on priority compared to big ones.
• Emergency department in any hospital handles patients based on their severity, thus priority queue could
used to implement such logic.
Notes
Binary Heap can be used for solving algorithmic problems, like the following -
• Finding top 10 most frequently used words in a very large file in O(n)
• Finding top 1 million numbers from a given large file containing 5 billion numbers in O(n)
• You have a file with 1 trillion numbers, but memory can hold only 1 million, How would you find the lowest 1
million out of all these numbers?
Hint - Create a binary-max-heap with 1 million capacity, so the largest number will be at the top. Now go
over each number in the file, compare it with the peek(), if it is smaller then poll() the element and add the
new one. The total complexity should be less than O (n log n). Selection Rank algorithm could also be used
to solve this problem, provided there exists no duplicate number.
Collections.sort() internally calls Arrays.sort() and thus the underlying algorithm for both of these methods is
same. The only difference is the type of input these methods accept.
Merge Sort algorithm is used by Arrays.sort() method as of JDK 6.
Q 186. There are 1 billion cell-phone numbers each having 10 digits, all of them
stored randomly in a file. How would you check if there exists any duplicate? Only 10
MB RAM is available to the system.
Approach 1
Hash all these numbers into 1000 files using hash(num)%1000, then the duplicates should fall into the same
file. Each file will contain 1 million numbers roughly, then for each file use HashSet to check for the duplicates.
Q 187. What is a Binary Search Tree? Does Java provide implementation for BST?
How do you do in-order, pre-order and post-order Traversal of its elements?
TreeMap in Java 6
Java provides its Binary Search Tree implementation in TreeMap class.
TreeMap is special kind of BST which
is height balanced and known as red- 8 Root Node
black-tree.
Tree Traversal
There are three types of depth-first 4 10
traversal, namely pre-order, in-order
and post-order.
Pre-order Traversal
• Visit the root node 2 5 9 12
• Traverse the left subtree
• Traverse the right subtree
In-order Traversal
• Traverse the left subtree 1 3
• Visit the root node
• Traverse the right subtree
Figure : Binary Search Tree Example
Post-order Traversal
• Traverse the left subtree
• Traverse the right subtree
• Visit the node
Pre-order
preOrder(node){
if(node == null)
return
visit(node)
preOrder(node.left)
preOrder(node.right)
}
In-order
1 http://en.wikipedia.org/wiki/Binary_search_tree
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 181
inOrder(node){
if(node==null)
return;
inOrder(node.left)
visit(node)
inOrder(node.right)
}
Post-order
postOrder(node){
if(node==null)
return;
postOrder(node.left)
postOrder(node.right)
visit(node)
}
Q 188. What is technique to sort data that is too large to bring into memory?
To sort data that is residing on secondary storage (disc, tape, etc) rather than in main memory (primary
storage), we use a sorting technique that is called external sort. There could be two different scenario where
data can not fit into main memory -
• Items to be sorted are themselves too large to fit into main memory (files, images, audio, video, etc), but
there are not many items. In this case we can only sort the keys and a value indicating the location of data
on disc. After the key-value pairs are sorted as per required criteria, the data is rearranged on disc into
correct order.
• Items to be sorted are too many to fit into main memory at one time, but the items themselves are small
enough to fit into memory (age, employee data, dates, numbers, strings, etc). In this case the data can
be divided in to partitions that fit into main memory, and then resulting files can be merged into single file.
Merge Sort or Radix sort can be used as external sorting technique in this case.
Q 190. How would you convert a sorted integer array to height balanced Binary
Search Tree?
Input: Array {1, 2, 3}
Output: A Balanced BST
2
/ \
1 3
Algorithm
1. Get the middle element of the sorted array (start+(end-start)/2) and put this element at the root
2. Recursively repeat the same for the for the left and right child
i. Get the middle of the left half and make it left child of the root created in step 1.
ii. Get the middle of right half and make it right child of the root created in step 1.
Java Source
public class SortedArrayToBST {
static class Node {
Node left;
Node right;
int data;
}
factorial(n) = n x factorial(n-1)
Recursive Algorithm
1. Base Condition - factorial of 1 is equal to 1.
2. Recursive Condition - factorial of n = n*factorial(n-1).
Be noted that integer variable can hold maximum value of 231, which will overflow once number is large. To fix that issue,
you can use BigInteger class that can hold relatively large numbers.
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 184
Q 193. You have a mixed pile of N nuts and N bolts and need to quickly find the cor-
responding pairs of nuts and bolts. Each nut matches exactly one bolt, and each
bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is
bigger. But it is not possible to directly compare two nuts or two bolts. Given an effi-
cient method for solving the problem.
A simple modification of Quicksort shows that there are randomized algorithms whose expected number of
comparisons (and running time) are O(n log n).
Approach
Pick a random bolt and compare it to all the nuts, find its matching nut and compare it to all the bolts (they
match, the nut is larger, the nut is small). This will divide the problem into two problems, one consisting of nuts
and bolts smaller than the matched pair and the other consisting of larger pairs. Repeat and divide this until all
the nuts and bolts are matched. This is very similar to quick sort in action and achieving the result in O(n log n)
time.
References
http://www.wisdom.weizmann.ac.il/~naor/PUZZLES/nuts_solution.html
http://algs4.cs.princeton.edu/23quicksort/
Q 194. Your are give a file with 1 million numbers in it. How would you find the 20
biggest numbers out of this file?
For more details about Selection Algorithm, please refer to below link
http://en.wikipedia.org/wiki/Selection_algorithm
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 185
Q 195. Reverse the bits of a number and check if the number is palindrome or not?
Answer : int numBitsReversed = Integer.reverse(num);
then XOR the number with the reversed number if zero then palindrome
Java Source1
private void mirror(Node node) {
if (node != null) {
// do the sub-trees
mirror(node.left);
mirror(node.right);
1 http://cslibrary.stanford.edu/110/BinaryTrees.html#java
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 186
Exponentiation is a mathematical operation, written as bn, involving two numbers, the base b and the exponent
n. When n is a positive integer, exponentiation corresponds to repeated multiplication of the base: that is, b^n
is the product of multiplying n bases i.e.
1 if n = 0;
1/x-n if n < 0;
x n
x.(xn-1/2)2, if n is odd
(xn/2)2, if n is even
Program Output
Calculating exponent for base - 2
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 187
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
Notes
Time Complexity of this algorithm is O (log n) where n is the exponentiation. The number of multiplications
reduces to half on each iteration.
Another method for calculating the pow
Other way to write the same recursive algorithm,
int power(int x, int y) {
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
void test() {
int x = 2;
int y = 3;
System.out.println(power(10, 3));
}
But the time complexity in this case would be O (n) and space complexity O (1), above method can be further
optimized to O (log n) by calculating the power(x , y/2) only once and storing it, also taking care of negative y
values as well -
int power(int x, int y) {
if (y == 0)
return 1;
else {
int temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else {
return y > 0 ? x * temp * temp : (temp * temp) / x;
}
}
}
T dequeue(){
if(first!=null){
T item = first.data;
first = first.next;
return item;
}
return null;
}
public static void main(String[] args) {
QueueUsingNode<Integer> test = new QueueUsingNode<>();
test.enqueue(100);
System.out.println("test = " + test.dequeue());
}
}
Node<T> top;
T pop() {
if (top != null) {
T item = top.data;
top = top.next;
return item;
}
return null;
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 189
T peek() {
return top.data;
}
The below program can be used to test if given input is pangram or not. this method utilizes bitwise OR and
left shift operator to set nth bit in an integer. Since integer has 32 bits, so its sufficient to store and check if all
alphabets (26 in number) are present in a given sentence.
@Test
public void testPangramEfficient() {
String input = "The quick brown fox jumps over the lazy dog";
assertTrue(pangramChecker.checkPangramEfficient(input));
}
}
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 190
Q 201. How would you implement a simple Math.random() method for a given range
say (1-16)?
Generating random numbers is not that easy because there are lots of expectations from a perfect random
number generator (fair distribution, randomness, fast, etc). The scope of this question is just to write a simple
function without worrying about the fairness, speed.
In order to generate a random number, we would require a seed which provides us with the randomness.
System.currentTimeMillis() could be a good substitute for providing seed value in our case.
The value returned by the System.currTimeMillis() is very large and we need to make it fit to our bounds using
the modulus operator (x % n) which will bound the upper value to be less than n.
Q 202. How an elevator decides priority of a given request. Suppose you are in an
elevator at 5th floor and one person presses 7th floor and then 2nd presses 8th floor.
which data structure will be helpful to prioritize the requests?
Generally elevator's software maintains two different queues - one for the upward traversal and another for
downward traversal along with the direction flag which holds the current direction of movement. At any given
point in time, only single queue is active for the serving the requests, though both the queues can enqueue
requests.
PriorityQueue is used for storing the user requests where priority is decided based on following algorithm.
Requests are removed from the PiorityQueue as soon as they are served. If current floor is 5th and user
presses 4th floor with upward moving elevator, then the requests are queued to the downward movement
priority queue (which is not yet active)
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 191
Q 203. How would you multiply a number with 7 using bitwise hacks?
This can be achieved by multiplying the number with 8 and then subtracting the number from the result.
If we left shift bits of a number by 23 then it would be equivalent to multiplying the number by 8.
Q 204. What is the best way to search an element from a sorted Integer Array? What
would be it's time complexity?
Binary search is best when we want to search from within a sorted collection.
It narrows down the search area to half in each iteration and thus very time efficient.
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
1 http://datastructuresblog.wordpress.com/2007/03/30/reversing-a-single-linked-list-using-stack/
2 http://crackinterviewtoday.wordpress.com/2010/03/24/reverse-a-single-linked-list-recursive-procedure/
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 193
Q 206. How would you count word occurrence in a very large file? How to keep track
of top 10 occurring words?
Questions worth asking - can file fit into main memory?, how many distinct words are there in the file?
Please note that there are limited number of natural language words available and all of them can be easily fit
into today's computer RAM. For example oxford English dictionary contains total of around 0.6 million words.
Word Count
word1 100
word1 word2 word3 word2 word3 word2 49
word3 60
... ..
achieved by maintaining a binary min heap of max size K, and then for each word count in hashmap -
i. Check if the heap size if less than K - then add the new word count to min heap. Otherwise
ii. Check if the peek element (that is minimum value in binary min heap) is less than the new word count,
and if it is, then remove the existing number and insert the new word count into min heap.
iii. When we are done traversing the entire word-counts then we will have heap containing the top K
frequently occurring words.
@Override
public String toString() {
return "TopOccurrence{" + "minHeap=" + minHeap + ", maxSize=" + maxSize + '}';
}
}
The overall time complexity of the above algorithm should be O (n log k) where n is the total number of
elements and k is the number of top occurrence elements that we need. Space complexity would be Big O(k +
d), where d is the total number of distinct words in the file.
Notes
We preferred to choose Binary Heap over TreeSet because TreeSet provide a get method with Big O(log
n) time complexity over PriorityQueue's peek() method with Big O(1), so its a big time saver for the given
requirement.
Binary Min Heap1 is a complete binary tree2 data structure in which each Node is less than or equal to each of
its children. Heap is very efficient O(1) for finding minima and maxima from a given data set. PriorityQueue in
JDK 1.6 is a implementation for Binary Min Heap.
1 http://en.wikipedia.org/wiki/Heap_%28data_structure%29
2 http://en.wikipedia.org/wiki/Complete_Binary_Tree
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 195
2. Traverse the hash and find the with maximum frequency, "second":100 in this case, then create an array of
string with size 100 - this is Big O(x) where x number of unique words in hashmap
3. Now Traverse the hash again and use the number of occurrences of words as array index and append the
word at that position in the array, we will get something like this -
4. Then just traverse the array from the end and collect the k non null words. Time Complexity - Big O(k)
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
TRIE - memory is shared between multiple words with common prefix and word count can be maintained along
with the word termination mark, but it would be more time consuming than the HashMap
Approach 4 : Diving Data File into multiple if unique words are too big
If the number of unique words are too big to fit into main memory of computer, then we can use hashing
technique to divide the words into different files and start processing those files one by one.
Pseudo algorithm
• Create N output files
• Fetch words from big data file
• For each word, calculate the hashcode
• output file sequence = hashcode%N (where N is appropriately set according to the main memory available)
• Now all duplicate words will go into same file, so we can count the word frequency from each output file
separately and append them into a final merged file.
Alternatively run the below command in a text file containing multiple words in single line -
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 197
tr -s '[[:punct:][:space:]]' '\n' < words | sort | uniq -c | sort -nr | head -10 | awk '{print $2}'
The output will be something like shown below (if you remove awk command)-
2 you
1 when
1 visit
1 today
1 name
1 my
Useful discussion -
http://stackoverflow.com/questions/12190326/parsing-one-terabyte-of-text-and-efficiently-counting-the-number-
of-occurrences
Both of these classes implement Map interface and are part of Collections framework as of JDK 2. Hashtable
implements one extra interface - Dictionary which Hashmap does not.
HashMap should be preferred if thread-safety is not required, and ConcurrentHashMap should be preferred to
Hashtable if highly concurrent implementation is required.
ListIterator - It is a specialized iterator for lists that allows to traverse the list bidirectionally, modify the list
during iteration, and obtain the iterator's current position in the list. It allows complete modification - remove,
add and update operations are provided.
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 198
Q 209. What do you understand by Token Bucket Algorithm. What is its use?
Token Bucket Algorithm
Token bucket algorithm is used to define the upper limits on bandwidth and burstiness on the data transmission
in a software application. The token bucket algorithm is based on an analogy of a fixed capacity bucket into
which tokens, normally representing a unit of bytes or a single packet of predetermined size, are added at a
fixed rate.
Applications
1.) To provide download bandwidth limits in software applications like torrent & download managers.
2.) To control the download speed on 3G network by our cellular provider.
Implementation
Lets try to create an implementation for this algorithm. We will choose a Leaky Bucket Implementation,
where a fixed amount of tokens are filled after a
predefined interval into the bucket. If no one utilizes
those token, then they do not get accumulated
over time, they just over flow after the capacity
of bucket is reached. Let's name this strategy as
FixedIntervalRefillStrategy.
1. ) Refill Strategy
2. ) Maximum Capacity of Tokens - this is the
maximum amount of tokens that a client can ask for,
otherwise an exception is thrown.
3.) Size - it is the current size of the bucket which
will keep on changing as it is refilled after specific
interval and emptied by the clients.
@Override
public String toString() { return "Capacity : " + capacity + ", Size : " + size; }
}
public static TokenBucket newFixedIntervalRefill(long capacityTokens, long refillTokens, long period, TimeUnit unit)
{
TokenBucket.RefillStrategy strategy = new FixedIntervalRefillStrategy(refillTokens, period, unit);
return new TokenBucket(capacityTokens, strategy);
}
}
/**
* Create a FixedIntervalRefillStrategy.
*
* @param numTokens The number of tokens to add to the bucket every interval.
* @param interval How often to refill the bucket.
* @param unit Unit for interval.
*/
public FixedIntervalRefillStrategy(long numTokens, long interval, TimeUnit unit) {
this.numTokens = numTokens;
this.intervalInMillis = unit.toMillis(interval);
this.nextRefillTime = new AtomicLong(-1L);
}
@Override
public long getIntervalInMillis() {
return intervalInMillis;
}
}
Q 210. How will you implement fibonacci series using Iterative & Recursive approach
in Java 8?
public Fibonacci2() {
cache.put(0, 0L);
cache.put(1, 1L);
}
Fibonacci(int n) {
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 202
this.n = n;
}
Q 211. How will you write a multi-threaded HttpDownloader program using Java 8?
We can utilize parallel Stream API provided in Java 8 along with the ForkJoinPool to download the http urls
using multiple threads, below is the non-production implementation of the same.
package org.shunya.crackingjavainterviews;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.atomic.AtomicInteger;
Q 212. How will you find first non-repeatable character from a String using Java 8?
Below is the code to find the first non-repeating letter/character from a given string using Java.
package org.shunya.interview.example6;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
Q 213. How will you find Word Frequency in sorted order for a collection of words?
There are different mechanisms in Java 8 to get the word frequency, broadly they all can be categorized into 4
different categories -
(toMap , groupingBy) x (serial, concurrent)
}
public static void allMethod(List<String> words) {
Map<String, Long> frequency = words.stream().collect(groupingBy(Function.identity(), counting()));
System.out.println("frequency = " + frequency);
}
Concurrent version should be ideally faster on multi-processor machines.
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 206
Q 214. How will you calculate MD5 hash of a given String in Java?
Java provides MessageDigest class for calculation of MD5, SHA, etc.
Q 215. There is a set of integer values representing the time consumed by a job ex-
ecution in seconds, how will you find average execution time ignoring the extreme run
times (0 seconds or a value much above the average execution time)?
There are many strategies to exclude the bad entries from the input data, in order to calculate the accurate
average time for Job execution.
1. Compute the average using all the entries in first phase, then in second phase discard all those entries
from input that deviate more than permissible limit, then in third phase recompute the average again.
2. First discard N highest and lowest values and compute arithmetic mean for the rest. Set N to suitable value
so that, for example 1% or 10% of values are discarded.
3. Sort the input data in increasing order, then use the median, or middle value, or average of few middle
values.
4. Use geometric mean that give less weight for the outliers.
http://en.wikipedia.org/wiki/Geometric_mean
http://en.wikipedia.org/wiki/Median
http://en.wikipedia.org/wiki/Mean
Q 216. There are millions of telephone numbers in a country, each state of the
country has specific phone number range assigned to it. How will you determine which
state a number belongs to?
For the sake of simplicity lets take 8 digit phone number and each assume that each state has the range
shown in below format -
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 207
Now to solve such problem we can utilize the number range search algorithm. In Java we have ready made
TreeMap implementation that can also effectively solve this problem. Please note that all the phone number
ranges are mutually exclusive i.e. no phone number can belong to two states. We can use TreeMap's
floorEntry(int) method to find the applicable range for a given input.
floorEntry() Returns a key-value mapping associated with the greatest key less than or equal to the given key,
or null if there is no such key.
import java.util.TreeMap;
public class PhoneRangeSearch {
private class PhoneRange {
int start;
int end;
String state;
@org.junit.Test
public void testGetState() throws Exception {
PhoneRangeSearch phoneRangeSearch = new PhoneRangeSearch();
phoneRangeSearch.addRange(91000000, 91999999, "HP");
phoneRangeSearch.addRange(92000000, 92999999, "JK");
phoneRangeSearch.addRange(93000000, 93999999, "PB");
phoneRangeSearch.addRange(94000000, 94999999, "HR");
phoneRangeSearch.addRange(95000000, 95999999, "UP");
assertEquals("HR", phoneRangeSearch.getState(94251212));
assertEquals("PB", phoneRangeSearch.getState(93158200));
assertEquals("HP", phoneRangeSearch.getState(91543543));
assertEquals("JK", phoneRangeSearch.getState(92100004));
}
Chapter - Algorithms & DS Cracking Java Interviews (Java 8, Hibernate & Spring) 208
From the above series we can see that there is a maxima-minima values. If we calculate the maximum and
minimum value from the input series in the first iteration (Big O N), then we can utilize those upper/lower bound
values to check if the given input is contained in this bound or not.
Java Source
import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;
/**
* Created by Munish on 4/18/2015.
*/
public class MaximaMinimaSeries {
public static void main(String[] args) {
int [] input = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1};
final IntSummaryStatistics summaryStatistics = IntStream.of(input).summaryStatistics();
final int max = summaryStatistics.getMax();
final int min = summaryStatistics.getMin();
Chapter 5
Object Oriented Design
Q 218. What are the key principles when designing a software for performance effi-
ciency?
1. Stateless design using REST can help achieve scalability whereever possible. In such application, minimal
session elements need to be replicated while distributing the application over multiple hosts. Users can
save their favorite URLs and thus there should be no need for the page flow, if we use REST.
2. Logging can be done asynchronously to save precious time of a method call.
3. More processes vs more threads can be configured based on the demand of the target application.
Generally it is advised to have a JVM with up to 2 GB memory because increasing memory beyond 2 GB
incurs heavy GC pauses, and if we require more processing then we prefer to have a separate process
for the JVM altogether. Multiple independent tasks should be run in parallel. Tasks can be partitioned to
improve the performance.
4. If we improve upon the concurrency of the software piece, then we can increase its scalability. This can be
achieved by reducing the dependency on the shared resources. We should try utilizing the latest hardware
optimization through JAVA as much as possible. For example we can use Atomic utilities provided in java.
util.concurrent.atomic package, or Fork & Join to achieve higher throughput in concurrent applications. We
should try holding the shared locks for as little time as possible.
5. Resource pooling and caching can be used to improve the processing time. Executing jobs in batches can
further improve the performance.
6. Picking up appropriate algorithm and data structure for a given scenario can help optimize the processing.
7. If we are using SQL in our application then we should tune the SQL, use batching whereever possible and
create indexes on the essentials table columns for faster retrievals.
8. We should tune our JVM for optimum memory settings (Heap, PermGen, etc) and Garbage collection
settings. For example if we do lot of text processing in our application with big temporary objects being
created, then we should have larger Young Generation defined so that frequent gc run does not happen.
9. Keep up to date with new technologies for performance benefits.
Q 219. How would you describe Producer Consumer problem in Java? What is its sig-
nificance?
• Producer can keep filling the Work Queue irrespective there is a Consumer available to immediately
process it. Queue Capacity will determine the number of Work Items it can hold.
• Consumer can be added on demand i.e. if there is more work items in the Queue then we can add more
consumers to the same shared Queue and thus share the work load.
• In a similar way N number of producer can be attached the Queue.
• Queue makes sure that no work item is picked up by more than one consumer thread. Java's thread
synchronization mechanism can be utilized to achieve thread-safety.
Using Java 1.5 onwards, this problem can be easily solved using Thread and shared BlockingQueue
implementation, as demonstrated below.
import java.util.concurrent.LinkedBlockingQueue;
/**
* Created by Munish on 4/14/2015.
*/
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 211
Consumer(CloseableBlockingQueue q) {
queue = q;
}
void consume(Object x) {
System.out.println(x);
}
}
Producer(CloseableBlockingQueue q) {
queue = q;
}
Object produce() {
return new Object();
}
}
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 212
class Engine {
public static void main(String[] args) throws InterruptedException {
CloseableBlockingQueue<Integer> q = new CloseableBlockingQueue<>();
Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
Thread.sleep(1000);
q.setClosed(true);
}
}
Q 220. How would you implement a Caching for HttpDownloader Task using Decora-
tor Design Pattern?
Decorator Design pattern makes it very easy to enrich the behavior of an existing class by adding wrapper over
it, thus maintaining the loose coupling at the same time. Let's first discuss the overall design for implementing
caching to Real Http Downloader Task.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;
import java.util.concurrent.ConcurrentHashMap;
@Override
public File download(URI uri, String fileName) throws IOException {
Path path = Paths.get(fileName);
long totalBytesRead = 0L;
HttpURLConnection con = (HttpURLConnection) uri.resolve(fileName).toURL().openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(10000);
StandardOpenOption.WRITE));) {
totalBytesRead = fileChannel.transferFrom(rbc, 0, 1 << 22); // download file with max size 4MB
System.out.println("totalBytesRead = " + totalBytesRead);
fileChannel.close();
rbc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return path.toFile();
}
}
interface HttpDownloader2 {
public File download(URI uri, String fileName) throws IOException;
}
@Override
public File download(URI uri, String fileName) throws IOException {
if (cache.contains(uri))
return cache.get(uri);
return delegate.download(uri, fileName);
}
}
Here we see that CachedHttpDownloader implements the same interface HttpDownloader and it has a
delegator object which holds the instance of RealHttpDownloader which if required download from the Http.
Gathering Requirements
1. Authentication for the valid user
2. Managing (add, update, remove) the Inventory of all type of publications (Book, Journal, Magazine, etc)
3. Ability to search for a Publication by various parameters
4. Ability to borrow a Book from library
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 215
Similarly borrow, return and renew are the type of transaction that a library user will typically be performing.
Transaction can be made an interface and Return, Borrow & Renew will implement this interface.
Gathering Requirements
1. Authenticate user with PIN
2. Select account type - Current Account, Saving Account
3. Select Operation : Balance Inquiry, Withdrawal or Deposit
4. Execute any of the above operation after supplying necessary input.
5. Print the transaction if required.
Design
We can utilize state design pattern for 2nd step mentioned above to maintain the state of account selected by
the user.
Account has two states - Saving Account & Current Account. User will select one of these account for
executing a transaction, and the appropriate state will be set at that moment.
3rd step can be solved using polymorphism where Balance Inquiry, Withdrawal & Deposit represents a
Transaction which can be executed.
Prompt user if the user requires receipt or not and accordingly execute the action.
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 217
You can Create a ThreadPoolExecutor and a CallableTask which can fetch URL from the above created Queue
and get the HTTP contents and index/crawl them. HttpClient can be used (JSoup, HtmlUnit, etc can also be
used) to fetch the contents/links from a web page. IOChannels can be utilized to download the HTML contents
from a URL in an efficient manner.
Java 7's Fork and Join Pool framework can give a better throughput for the same hardware. We will cover a
simple implementation using ForkJoinPool framework instead of ExecutorService.
Java Source
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ForkJoinPool;
public WebsiteCrawler(String startUrl, int maxThreadCount, int maxDepth, int maxBreadth) throws
URISyntaxException {
this.inputUrl = startUrl;
this.linkTracker = new WebsiteLinkTracker(maxDepth, maxBreadth, getDomainName(startUrl));
this.pool = new ForkJoinPool(maxThreadCount);
}
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 218
package org.shunya.crawler;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ForkJoinPool;
public WebsiteCrawler(String startUrl, int maxThreadCount, int maxDepth, int maxBreadth) throws
URISyntaxException {
this.inputUrl = startUrl;
this.linkTracker = new WebsiteLinkTracker(maxDepth, maxBreadth, getDomainName(startUrl));
this.pool = new ForkJoinPool(maxThreadCount);
}
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.RecursiveAction;
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 220
@Override
public void compute() {
if (!tracker.visited(url) && tracker.checkDepth(currentDepth)) {
try {
List actions = new ArrayList();
Set<String> allLinks = getAllLinks(url);
allLinks.stream().limit(tracker.getMaxBreadth()).forEach(link -> {
if (!link.isEmpty() && !link.endsWith(".pdf") && !link.endsWith(".jpg") && link.contains(tracker.
getDomain()) && !tracker.visited(link)) {
actions.add(new LinkSearcher(currentDepth + 1, link, tracker));
} else if (link.endsWith(".pdf")) {
System.out.println(currentDepth+" : found --> " + link);
}
});
tracker.addVisited(url);
invokeAll(actions);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Q 224. Design Phone Book for a mobile using TRIE (also known as prefix tree).
TRIE is an ordered tree data structure which store associative arrays and the keys are usually alphabets.
The position in the tree defines the key with which it is associated. The root node is generally empty (\0) and
it contains upto 26 children each representing a alphabet character. All descendants of a given node have a
common prefix of string associated with that node. Each node contains a flag which tells if the current node is
full word or not. In the diagram on right, pink colored boxes shows the full words.
The term trie comes from retrieval
TRIE is a generally good data structure for
storing dictionary like data. Phone book can
\0
be perfectly implemented using TRIE which
will save memory as well as time for prefix
searching. A B C D E ... Z
A typical Node of a Trie is defined as
Q 225. How would you resolve task's inter dependency, just as in maven/ant.
Let's consider the following task dependencies.
Task Dependent On
3 1,5
2 5,3
4 3
5 1
Here first row states that task 3 is dependent on task 1 and task 5, and so on. If the two consecutive tasks
have no dependency, then they can be run in any order.
The result should look like - [1, 5, 3, 2 ,4] or [1, 5, 3, 4, 2]
Approach 1
It is a typical Graph traversal problem, that can be solved using Topological Sorting Algorithm1 in linear time
O(|V| + |E|),
Where
V = number of nodes
E = number of edges
2 4
5 3
1
Direct Acyclic Graph for the Task Dependencies
First, find a list of "start nodes" which have no outgoing edges and insert them into a set S; at least one such
node must exist in an acyclic graph.
1 http://en.wikipedia.org/wiki/Topological_sorting
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 224
while S is non-empty do
remove a node n from S
insert n into L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
Java Source
//while S is non-empty do
while(!S.isEmpty()){
//remove a node n from S
Node n = S.iterator().next();
S.remove(n);
//insert n into L
L.add(n);
if(cycle){
System.out.println("Cycle present, topological sort not possible");
}else{
System.out.println("Topological Sort: "+ Arrays.toString(L.toArray()));
}
}
}
Approach 2
We can use HashMap to solve this problem.
Q 226. How would you sort 900 MB of data using 100 MB of RAM?
External sort can be used for sorting data that can not fit into main memory.
Algorithm1
External Merge Sort is the answer to the above mentioned problem.
1. Read 100 MB of data in main memory and sort by some conventional method like quicksort.
2. Write the sorted data to the disk.
3. Repeat step 1 & 2 until all the data is in sorted 100 MB chunks (9 chunks) which now need to be merged
into single output file.
4. Read first 10 MB of each sorted chunk into input buffer in main memory and allocate remaining 10 MB for
the output buffer.
5. Perform 9 way merge and store the result in output buffer.
Similar Questions
Question2
There are 2 huge files A and B which contains numbers in sorted order. Make a combined file C which contains
the total sorted order.
Solution
Merge Sort technique.
Question
There are k files each containing millions of numbers. How would you create a combined sort file out of these?
Solution
• Use a binary-min heap (increasing order, smallest at the top) of size k, where k is the number of files.
• Read first record from all the k files into the heap.
• Loop until all k files are empty.
• Poll() the minimum element from the binary heap, and append it to the file.
• Read the next element from the file from which the minimum element came.
• If some file has no more element, then remove it from the loop.
• In this way we will have one big file with all number sorted
• Time complexity will be O (n log k)
1 http://en.wikipedia.org/wiki/External_sorting#External_merge_sort
2 http://en.wikipedia.org/wiki/Merge_sort
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 228
private void splitFileAndSort(int start, int maxSize, Path inputFile, Path outputFile) throws IOException {
try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(outputFile.toFile())))) {
Files.lines(inputFile).skip(start).limit(maxSize).parallel().mapToInt(Integer::valueOf).sorted().forEachOrdered(s
-> pw.println(s));
}
}
private void mergeResults(List<Path> pathList, Path outFile, int numPartitions, int recordCount) throws
IOException {
int[] topMost = new int[numPartitions];
BufferedReader[] fileBuffers = new BufferedReader[numPartitions];
for (int i = 0; i < numPartitions; i++) {
fileBuffers[i] = new BufferedReader(new FileReader(pathList.get(i).toFile()), 1000);
loadNext(topMost, fileBuffers, i);
}
CreateTestData() -> Creates a new file with specified number of random numbers in it.
splitAndSort() -> sorts a chunk of data and write it to a file at a time
main() -> driver program
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 230
Q 227. How would you design minimum number of platforms so that the buses can
be accommodated as per their schedule?
BUS Arrival Time (HRS) Departure Time (HRS)
A 0900 0930
B 0915 1300
C 1030 1100
D 1045 1145
E 1100 1400
Bus Schedule for a given Platform
This problem is about finding the peak time when maximum number of buses are waiting to get into platform.
Ideally we would not like to stop a bus outside the platform so every single bus would require one platform.
So in this problem, the maximum number of buses arriving at the same time during the peak time of day will
decide the number of platforms.
Pseudocode
Calculate the peak time of the buses from the given bus schedule, the number of buses at the peak time will
give us the number of platforms.
Step 1
Create a single array of bus timing after append A (for Arrival) and D (for departure) to each bus time.
So the above table should now become a one dimensional array like this :
0900A 0930D 0915A 1300D 1030A 1100D 1045A 1145D 1100A 1400D
Step 2
Sort the Bus Time (above array) in ascending order (Natural Order)
Create a Counting Array (mark +1 for Arrival, -1 for Departure)
0900A 0915A 0930D 1030A 1045A 1100A 1100D 1145D 1300D 1400D
Step 3
+1 +1 -1 +1 +1 +1 -1 -1 -1 -1
Create a array with Prefix Sum f(x) = x + (x-1), it gives total amount of buses at a given time. Prefix sum gives
us the cumulative running total at any given array index i.e. the cumulative sum of all buses present at a
station at a given time.
Now traverse the entire array and find the index with maximum value, this value is maximum number of Buses
1 2 1 2 3 4 3 2 1 0
at peak time.
Thus we can see that minimum number of platforms required is 4 at 1100 when there are 4 buses present at
the platform.
Pseudo Code
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 231
Java Source
public class PlatformDesigner {
static class BusSchedule {
final String name, arrivalTime, departureTime;
BusSchedule(String name, String arrivalTime, String departureTime) {
this.name = name;
this.arrivalTime = arrivalTime;
this.departureTime = departureTime;
}
}
private List<BusSchedule> busScheduleList = new ArrayList<>();
public void addBusSchedule(BusSchedule schedule) {
busScheduleList.add(schedule);
}
public void calculateNoOfPlatforms(){
List<String> scheduleTokens = new ArrayList<>();
for (BusSchedule schedule : busScheduleList) {
scheduleTokens.add(schedule.arrivalTime+"A");
scheduleTokens.add(schedule.departureTime+"D");
}
Collections.sort(scheduleTokens);
int max=0, counter =0;
String peakTime ="";
for (String token : scheduleTokens) {
if(token.endsWith("A")){
counter++;
}else if(token.endsWith("D")){
counter--;
}
if(counter > max){
max=counter;
peakTime = token;
}
}
Q 228. There is a pricing service which connects to Reuters & Bloomberg and
fetches the latest price for the given Instrument Tics. There could be multiple price
events for the same Stock and we need to consider the latest one. Design a service to
show prices for the Top 10 stocks of the Day?
This problems requires us to collect price feeds from PricingService, remove duplicates based on InstrTic
keeping the latest one, and then finally capture the top 10 feeds based on price. Keeping in mind that we
need to remove duplicates based on Tic# and then sorting based on price i.e. 2 different fields for two different
operations.
Java Source
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by Munish on 4/19/2015.
*/
public class PricingService {
Random rand = new Random(System.currentTimeMillis());
/**
* Generates & Returns a Random Feed of Stock Information with variant Price information.
* @return
*/
public List<Stock> getPriceFeed() {
int[] instrTics = {100, 1001, 1002, 1003};
String[] names = {"Reliance", "TCS", "Airtel", "BSNL"};
Random randomStraem = new Random(System.currentTimeMillis());
double[] prices = randomStraem.doubles(100.0, 200.0).limit(50).toArray();
List<Stock> results = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final int randInt = randInt(0, instrTics.length-1);
results.add(StockBuilder.aStock().withInstrTic(instrTics[randInt]).withPrice(prices[randInt(0, prices.length-1)]).
withName(names[randInt]).build());
}
return results;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Stock stock = (Stock) o;
return instrTic == stock.instrTic;
}
@Override
public int hashCode() {
return instrTic;
}
@Override
public String toString() {
return "Stock{" +
"price=" + price +
", instrTic=" + instrTic +
", name='" + name + '\'' +
'}';
}
}
import java.util.*;
import java.util.function.Consumer;
@Override
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 235
Q 229. Design a parking lot where cars and motorcycles can be parked. What data
structure to use for finding free parking spot in Parking Lot program? Assume there
are million of parking slots.
Approach
1. Create model classes to OO map Vehicle, vehicleType, Slot, SlotSize, etc.
2. Create parking manager which will track the free parking slots using a Queue for fast retrieval, and occupied
vehicle mapping will be stored using a HashMap for fast O(1) retrieval. Whenever a slot gets free, remove it
from the HashMap and add it into Queue, and if new vehicle comes in then pick slot from the head of queue
and store the mapping in hashmap. Separate Queue & HashMap could be used for Motor Bike, Truck & Car
vehicle type.
Notes
A free list is a data structure used in a scheme for dynamic memory allocation. It operates by connecting
unallocated regions of memory together in a linked list, using the first word of each unallocated region as a
pointer to the next. It's most suitable for allocating from a memory pool, where all objects have the same size.
Free lists make the allocation and deallocation operations very simple. To free a region, one would just link it to
the free list. To allocate a region, one would simply remove a single region from the end of the free list and use
it. If the regions are variable-sized, one may have to search for a region of large enough size, which can be
expensive.
Maintain a PriorityQueue for free parking space, use hashmap for the filled spaces. In this manner it would be
easier to find the free space and to find the parked object. Assume that the parking space on the lower floor
gets more priority than the parking space on the higher floor, when a new car comes in just pick the top most
entry from the parking queue and park the object
Q 230. There is three file contains flight data, write a standalone program to search
flight detail from all files depend on criteria? Write JUnit to demonstrate the working.
BA412|BOS|CDG|31-12-2010|0210|7.50|800
BA413|BOS|AMS|30-11-2010|1530|7.00|750
• Write a parser to read the flight details from the flat file using pipe (|) as the delimiter. Class
FlightDataReader does this Job as shown in the coming pages.
• Create a Data Transfer Object (FlightSearchDTO) to store the flight related information in a collection, then
load all the flight data into arraylist.
• Index all flight data into HashMap using <src, dest> pair as the key of hashing data structure, it will allow
constant time Big O (1) search for a given src destination pair.
• Create a Fare comparator to sort flight search details based on fair details in ascending order.
• Write a Junit Test to run the demonstration of Program.
•
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
/**
* Created by Munish on 4/19/2015.
*/
public class FlightSearchService {
private Map<String, List<FlightSearchDTO>> srcDstFlightIndex = new HashMap<>();
private FlightDataReader flightDataReader = new FlightDataReader();
private List<FlightSearchDTO> allFlightData;
import java.io.IOException;
import java.nio.file.Files;
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 238
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Munish on 4/19/2015.
*/
public class FlightDataReader {
import java.util.Date;
return arrivalLocation;
}
@Override
public String toString() {
return "FlightSearchDTO{" +
"flightNo='" + flightNo + '\'' +
", departLocation='" + departLocation + '\'' +
", arrivalLocation='" + arrivalLocation + '\'' +
", validTillDate=" + validTillDate +
", flightTine=" + flightTine +
", duration=" + duration +
", fare=" + fare +
'}'+"\n";
}
}
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 241
import org.junit.Before;
import org.junit.Test;
import java.util.List;
@Before
public void setUp() throws Exception {
flightSearchService.loadFlightData();
}
@Test
public void testSearchFlights() throws Exception {
FlightSearchCriteria flightSearchCriteria = new FlightSearchCriteria();
flightSearchCriteria.setDepartLocation("DEL");
flightSearchCriteria.setArrivalLocation("AMS");
flightSearchCriteria.setSortByFare(true);
final List<FlightSearchDTO> flightSearchDTOs = flightSearchService.searchFlights(flightSearchCriteria);
assertThat(flightSearchDTOs.size(), equalTo(2));
System.out.println("flightSearchDTOs Search = " + flightSearchDTOs);
}
}
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 242
Q 231. Implement the classes to model two pieces of furniture (Desk and Chair) that
can be constructed of one of two kinds of materials (Steel and Oak). The classes repre-
senting every piece of furniture must have a method getIgnitionPoint() that returns the
integer temperature at which its material will combust. The design must be extensible
to allow other pieces of furniture and other materials to be added later. Do not use
multiple inheritance to implement the classes.
Design
Abstract Factory along with Bridge Pattern can solve this problem.
public interface Furniture {
public int getIgnitionPoint();
}
}
}
Notes
Q 232. How would you simulate a digital Clock in Object Oriented Programming
Language?
The simplistic design consists of creating a second hand, which when completes 60 seconds, advances the
minute hand by 1 minute. Similarly Hour hand is advanced by 1 hour when minute hand completes its 60
minutes. This can be emulated in software by registering MinuteHand as an Observer to SecondHand, and
HourHand as a observer to MinuteHand. The only real subject in this case is the Second Hand which keeps
ticking once every second.
ClockController is the class that manages the overall state of the clock and provide us the option to start & stop
the clock.
@Override
public void increment() {
count++;
if(count>=60){
count=0;
observer.increment();
}
}
@Override
public void display(){
System.out.println("seconds = " + count);
}
}
@Override
public void increment() {
count++;
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 246
if(count>=60){
count=0;
observer.increment();
}
}
@Override
public void display() {
System.out.println("minutes = " + count);
}
}
@Override
public void increment() {
count++;
if(count>=24){
count=0;
}
}
@Override
public void display() {
System.out.println("hours = " + count);
}
}
void display();
}
Chapter - OO Design Cracking Java Interviews (Java 8, Hibernate & Spring) 247
Q 233. How would you design an elevator system for multi story building? Provide
with request scheduling algorithm & Class diagram for the design.
For a single elevator system, normally two different queues are used to store the requests. One queue is
used to store upward requests and other queue used to store the downward requests. Queue implementation
used is the BlockingPriorityQueue which maintains the priority of its requests based on the floor numbers.
For upward motion, the lower floor number has the higher priority and opposite for the downward motion of
elevator. A 2 bit flag can be used to store the current direction of the elevator where 00 represents Idle, 01 for
upward motion, 11 for the downward motion.
A Bit Vector can be used to map the floor numbers, and if someone presses the floor button then the
corresponding bit can be set to true, and a request is pushed to the queue. This will solve the duplicate request
problem from outside the elevator at the same floor. As soon as the floor request is served, the corresponding
bit is cleared and the request is removed from the queue.
The actual software application for handling elevator requires lot of interaction with the hardware and is out of
scope for this book.
Q 234. Given two log files, each with a billion username (each username appended
to the log file), find the username existing in both documents in the most efficient
manner?
Pseudo Code
Q 235. Design DVD renting system, database table, class and interface.
In order to make a Online DVD rental Store we would require a database, web server (Jetty), hibernate layer to
access the database, Restful Webservices (Jersey), HTML and JavaScript for the GUI.
@Entity
@Table(name = "DVD")
class DVD {
final int charge;
final String name;
final String id;
String category;
}
class RentalFacade{
void rentDVD(DVD dvd) {}
void returnDVD(DVD dvd) {}
int calculateRent(DVD dvd) {return 0;}
@Path("/dvd")
public class MachineResource extends AbstractResource {
@GET
@Path("getAll")
@Produces({APPLICATION_JSON})
public Response getAllDVD() {
return Response.ok(fromContext(DAO_FACADE, RentalFacade.class).searchByCategory("")).build();
}
Chapter 6
Puzzles & Misc
Q 236. Why man holes are round in shape?
There are multiple reasons for that
1. Round shape is easy to machine compared to any other shape, thus it reduces the effort and cost.
2. Round objects are easy to move by rolling them, thus labour is reduced.
3. Round shape objects can't fell into the hole, for other shapes its not true.
As I hope you can see by now, if the solution lays somewhere in a floor low down, then we have extra-
headroom when we need to step by singles, but, as we get higher up the building, we’ve already used drop
chances to get there, so there we have less drops left when we have to switch to going floor-by-floor.
If it doesn’t break, rather than jumping up another n floors, instead we should step up just (n-1) floors (because
we have one less drop available if we have to switch to one-by-one floors), so the next floor we should try is
floor n + (n-1)
Similarly, if this drop does not break, we next need to jump up to floor n + (n-1) + (n-2), then floor n + (n-1) +
(n-2) + (n-3) …
We keep reducing the step by one each time we jump up, until that step-up is just one floor, and get the
following equation for a 100 floor building:
This summation, as many will recognize, is the formula for triangular numbers (which kind of makes sense,
since we’re reducing the step by one each drop we make) and can be simplified to:
x (x+1) / 2 >= 100
This is a quadratic equation, with the positive root of 13.651
Q 238. There are 100 doors all closed initially. A person iterates 100 times, toggling
the state of door in each iteration i.e. closed door will be opened & vice versa.
1st iteration opens all doors (1x multiplier)
2nd iteration closes 2,4,6,8 .. doors (2x multiplier)
3rd iteration opens 3,6,9,12 ... doors (3x multiplier) and so on.
In the end of 100 iterations, which all doors will be in open state?
A door will be left closed if even number of door toggling iterations for that particular door. For all odd number
toggling, the door state will be open.
For example,
The factors of 1 are 1 and itself. It has one factor - odd number of factors
The factors of 2, 3, and 5 are 1 and themselves. They have two factors - even number of factors
The factors of 4 are 1 and itself, as well as 2. 4 has three factors - odd number of factors
After analysis we can find that every number has even number factors except the number which are perfect
squares (and thus have odd number of factors).
1. Factors of 1 = 1x1; (no. of factors is 1) - door will be toggled only 1 time in very 1st iteration.
2. Factors of 4=1x4, 2x2, 4x1; (no. of factors is 3) - door will be toggled in 1st, 2nd and 4th iteration.
3. Factors of 9=1x9, 3x3, 9x1; (no. of factors is 3) - door will be toggled in 1st, 3rd and 9th iteration.
4. Factors of 16 = 1x16, 2x8, 4x4, 8x2, 16x1; (no. of factors is 5) - door will be toggled in 1st, 2nd, 4th, 8th and
16th iteration.
So in case of perfect squares, both the factors are same number, that trick will solve this problem.
So we can conclude that odd number of iterations will happen for all the numbers which are perfect square
numbers and those doors will be in Closed state (initially opened)
1,4,9,16,25,36,49,64,81,100
For every other room, door will remain Open after 100 iterations.
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 251
Q 239. What is probability of a daat, hitting closer to centre of a circle rather than cir-
cumference?
Answer is 25%
Let's understand this question using the figure shown here. Daat will hit closer to centre of circle than the
circumference when daat hits in a area whose radius is half the area of circle. The area of interest is shown in
blue color in the given figure, and total area is the blue + yellow area.
=1/4
=25%
Q 240. What is financial Instrument, Bond, equity, Asset, future, option, swap and
stock with example each?
Bond
A bond is a debt security under which the issuer owes the holders a debt and depending on the terms of the
bond, is obliged to pay them interest/coupon and to repay the principal at a later date, termed as maturity.
Stock
Constitutes the equity stake of its owners.
Equity
Equity is the residual claim or interest of the most junior class of investors in asset after all liabilities are paid.
Asset
Assets are economic resources. Anything tangible or intangible that is capable of being owned/controlled to
produce value and that is held to have positive economic value is considered an asset. In other words, Asset
represents value of ownership that can be converted into cash.
Capital = Assets - Liabilities
Coupling
Coupling is the degree to which each program module relies on each one of other module in the software
application.
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 252
Books
Design Patterns in Java - Head First
Concurrency In Practice by Brian Goetz
Effective Java 2nd Edition by Joshua Bloch
Algorithms 4th edition : http://algs4.cs.princeton.edu/home/
Cracking the Coding Interview
Technology Forums
http://www.geeksforgeeks.org/fundamentals-of-algorithms/
http://www.careercup.com
http://www.stackoverflow.com
Question :"given 1000 bottles of juice, one of them contains poison and tastes bitter. Spot the spoiled bottle in
minimum sips?"
Question : How would you detect a circular loop inside a linked list?
Question : How would you calculate size of a linked list having circular loop in it?
Circular Linked List - take 2 pointers
increment one by +1;
increment other by +2
they will first meet in N iterations
length of stem = n/2;
Question : There is a sorted Array of Integer but the array is rotated. How would you fix it using binary search?
why choose binary search?
http://leetcode.com/2010/04/searching-element-in-rotated-array.html
http://www.careercup.com/question?id=2800
http://xorswap.com/questions/77-implement-binary-search-for-a-sorted-integer-array-that-has-been-rotated
Question: How would you mirror a binary tree?
Question : I want to implement 2 different display score boards for the IPL cricket match, one specific to IPL
another for T20. Which design pattern will rescue you in this case?
Question : What is contract between equals() and hashcode() method?
Question : How would you write a hashcode() method for a class having two fields? Can we multiply hashcode
with a random number?
Question : Explain Fork and Join with concrete example?
http://www.oracle.com/technetwork/articles/java/fork-join-422606.html
http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html
http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html
http://fahdshariff.blogspot.in/2012/08/java-7-forkjoin-framework-example.html
http://www.javabeat.net/2012/06/simple-introduction-to-fork-join-framework-in-java-7/
http://www.vogella.com/articles/JavaConcurrency/article.html
Question : http://tech-queries.blogspot.in/2008/11/sort-array-containing-0-and-1.html
Question : Why wait() and notify() are at Object level rather than Thread level?
Answer : http://javarevisited.blogspot.in/2012/02/why-wait-notify-and-notifyall-is.html
Question : Why not to choose static factory method in place of singleton design pattern?
Question: There is an JPA entity having lazy loading items. You want to use this entity to render a view page
which will display this entity. What all options do you have to overcome the lazy loading in this case?
http://wiki.eclipse.org/Introduction_to_Mappings_(ELUG)#Indirection_.28Lazy_Loading.29
http://java.dzone.com/articles/jpa-lazy-loading
http://www.javacodegeeks.com/2012/07/four-solutions-to-lazyinitializationexc_05.html
Question : find median of two sorted array
Hint :http://www.geeksforgeeks.org/median-of-two-sorted-arrays/
Question : When no direct method is found, the most specific method is chosen by the JVM for a method call?
example?
Question : What are various techniques for achieving thread safety in Java : Immutable, ThreadLocal,
Synchronized access, non-blocking algo using CAS.
Question : http://www.geeksforgeeks.org/find-the-two-repeating-elements-in-a-given-array/
Question : Write a print method for printing Top N numbers from an array?
Question : Given a collection of Trades. Write an algorithm to remove duplicates based on Tic# and sorting
based on NAV.
Question : Design a vending machine.
Question : How would you implement a BoundedBuffer using Lock and Condition?
http://www.baptiste-wicht.com/2010/09/java-concurrency-part-5-monitors-locks-and-conditions/
Question : Give an example of timed locking using explicit locking in Java.
continued on 255
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 255
Hint : http://codeidol.com/java/java-concurrency/Explicit-Locks/Lock-and-ReentrantLock/
Question : Fibonacci Series using various techniques - recursive, iterative, Big O(1)
Question : Left Outer Join vs Right Outer Join?
Hint : http://en.wikipedia.org/wiki/Join_(SQL)#Inner_join
Question : Angle between Minute Hand and Hour hand of a clock?
Question : How is a TreeSet implemented in Java?
Question : How does google analytics works without causing a extra load on your web server?
Hint : Javascript is used to hit a google server with the required identifier then the new site is visited.
Question: How would you avoid data corruption by a web page which allows a update a database row, and 2
users try to update the same row simultaneously.
Question : How does batch update works in Java?
Question : Find the first common ancestor of two given nodes in a binary tree in o log n space complexity and
O(n) time complexity.
Hint - 1. do DFS, 2. during DFS if you find one of the nodes store the stack contents (path from the root) repeat
the same process for the second node. this requires 2nlogn space. 3. Now compare both of these paths from
the root, the last common node in the path is the first common ancestor. this takes logn time avg case and n in
worst case
Question : What is a BloomFilter? How is it better than hashmap in certain casess?
http://en.wikipedia.org/wiki/Bloom%5Ffilter
"Sort An Array Containing '0' And '1'
Sort An Array Containing '0','1' And '2'
http://tech-queries.blogspot.in/2008/11/sort-array-containing-0-and-1.html
Dutch flag algo."
http://en.wikipedia.org/wiki/Dutch_national_flag_problem
Discuss the numbering system.
traceroute command and nslookup
Natural ordering in search operation and stable search
TRIE
Sorting and Searching
Atomic package and CAS, non blocking algorithms
Database indexes clustered indexes, query plan etc, Outer and Inner Join
UNIX stuff cut grep ps etc, piping the output
Question : How would you implement a Trie in Java. Suppose you want to implement a Auto-suggest
functionality using Java where user presses a letter and all the words starting with that letter are listed in the
suggestion box. Which datastructure would you choose for this scenario?
Question: How would you implement ThreadPool in Java 1.4?
Question: How would you implement StringBuffer class, so that it doesn’t create un-necessary imtermediate
string objects upon modifications?
Question : Write a method to count the size of a LinkedList given a Node. There could be a circular loop inside
the list, hence your method should be smart enough to handle such situation gracefully.
Hint : http://crackinterviewtoday.wordpress.com/2010/03/16/loop-in-a-linked-list/
Question : How would you design a FileSystem for showing just the hierarchy of files? File Interface & then File
and DIR as the subclasses.
How would you map department and employee table into Java Objects? What kind of relationship would you
put there? Lazy loading?
Question : Which object construction mechanism you prefer in Spring DI - constructor based or setter based?
Hint- setter based injection is preferred mechanism for injecting dependencies, but at times constructor based
injection is preferred when mandatory dependencies need to be injected.
Question - We have list of One million numbers on which some mathematical function needs to be applied,
How would you make algorithm concurrent?
Continued on 256
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 256
Hint - You can use Executor framework for spawning multiple workers, and use a queue to feed the one million
input numbers. There could be another strategy where you divide the one million numbers into N parts and
feed each of these parts to one worker. You can also think of atomic package for handling such scenario.
There is an un-ordered stack of 5 elements and we have a method nextMinimum() which returns us the
subsequent next minimum element in O(1). suppose we have 2,3,1,4,5 as the elements, then first invocation
will return us 1, second 2, third 3.
Hint - maintain a queue which maintains the sorted references to the underlying stack.
What is Spring bean lifecycle?
What is embeddable in JPA
How do you performance tune an application - By GC, by changing algorithm, using different data structure
which is more appropriate for a given scenario.
Question: Design multi-player Chess Game using Class Diagrams.
Question: Design a Restaurant Reservation system.
Solution : http://www.careercup.com/question?id=15062886
Question: Design SkyDrive.
http://www.careercup.com/question?id=14692764
http://thought-works.blogspot.in/2012/11/object-oriented-design-for-cloud-based.html
Question: Design Online Auction Site.
Solution : http://thought-works.blogspot.in/2012/11/object-oriented-design-for-online.html
Question: Design a Train & reservation system. Give class structure and design UML
Solution :
http://www.careercup.com/question?id=3220674
Question: Write a 2 Thread application where one thread prints even number and the other thread prints odd
numbers and both of them act in a synchronized manner.
Question : Security and Performance Tuning of a REST and Ajax Application
http://www.oracle.com/technetwork/articles/java/securityperf-rest-ajax-177520.html
http://www.oracle.com/technetwork/articles/javaee/jax-rs-159890.html
Question : How does Tree Balancing works? left and right rotation?
Question : How does ReentrantReadWriteLock works internally?
Question : What do you understand by volatile keyword?
http://www.ibm.com/developerworks/java/library/j-jtp06197/index.html
Question : Why would you use Suffix Tree for searching?
Write a chapter on glossary. Mention few keywords used in Java and financial word. jargons
Can you tell me with example the Usage of ThreadLocal class? Calendar class, JDBC transaction
management, etc.
Question : Synchronization of getClass() in case of inheritance, will lock the actual class rather than the whole
hierarchy.
Question: How would you convert a sorted integer array to height balanced binary tree?
Question: Discuss about non-blocking algorithms using CAS?
https://www.ibm.com/developerworks/java/library/j-jtp04186/
Discuss the numbering system.
traceroute command and nslookup
TRIE and Sorting and Searching
Database indexes clustered indexes, query plan etc, Outer and Inner Join
UNIX stuff cut grep ps etc, piping the output
Question : What are the ways to achieve thread-safety in a concurrent program?
Question : How will you deal with ConcurrentModificationException?
Question : How to expose a method over JMX using MBean?
Thread.interrupt() puzzle.
Discuss on External Sorting
Continued on 257
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 257
http://www.careercup.com/question?id=83696
Question : Design a solution to print employee hierarchy in Java given a employee record.
Article : Java Software in Harmony with the Hardware - Mark Thompson
http://mechanical-sympathy.blogspot.in/2012/10/compact-off-heap-structurestuples-in.html
http://mechanical-sympathy.blogspot.in/2011/07/false-sharing.html
http://mechanical-sympathy.blogspot.in/2011/12/java-sequential-io-performance.html
Question : How would you find kth highest number in a list of n Numbers?
http://en.wikipedia.org/wiki/Selection_algorithm
http://stackoverflow.com/questions/3628718/find-the-2nd-largest-element-in-an-array-with-minimum-of-com-
parisom
http://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-
n-in-on
Question: Design Coffee maker (Vending Machine)..provide some class diagram
Solution : http://www.careercup.com/question?id=3171714
http://stackoverflow.com/questions/7067044/java-algorithm-to-solve-vendor-machine-change-giving-problem
Question: How do you represent the following expression in "class design": (5*3)+(4/2)? How would an
algorithm that computes the value of this expression work?
http://www.careercup.com/question?id=65911
Question : How would you design Money class, which holds currency as well as amount of money?
Hint : http://stackoverflow.com/questions/1359817/using-bigdecimal-to-work-with-currencies
http://stackoverflow.com/questions/11434938/display-curreny-value-in-india-as-rs-100-using-java
http://www.javapractices.com/topic/TopicAction.do?Id=13
How would you perform tree rotation to balance a tree.
Question : Write a function which logs/writes messages to files asynchronously . Multiple thread should be
able to write to different files concurrently e.g. if thread A want to write to a file ‘FA’ at the same time thread B
wants to write to a file ‘FB’ then both threads should be able complete operation concurrently. Threads which
wants to write messages to file shouldn’t block for file related i/o .
Sample interface.
Log{
void log(filename, message);
}
http://mentablog.soliveirajr.com/2013/02/inter-socket-communication-with-less-than-2-microseconds-latency/
http://mentablog.soliveirajr.com/2012/12/asynchronous-logging-versus-memory-mapped-files/
http://mentablog.soliveirajr.com/2012/11/inter-thread-communication-with-2-digit-nanosecond-latency/
Question : Design a price making system for a wholesale dealer, where user can subscribe/un-subscribe
online for any product to receive the real time prices. System will internally subscribe to different vendors to get
the product prices, aggregate them and return the best price to customer. Vendors may broadcast new prices
every few seconds and customers would like to see all the price updates until he un-subscribe for that product.
Dealer may also like to add some commission on every product price to remain in business and make some
profit.
Assume system will have limited number of vendors and products but can have high number of concurrent
customers requesting for product prices.
Primary concern for the customers to have minimal latency in the price updates.
Question : U are given binary search tree. How will you check whether it is balanced or not.
Question : U have UI and service. UI making 5000 request and service can handle only 500 request. I am okay
with slow response. But how will make sure all 5000 requests are processed
Question : U have a tree with each node has link to its parent. You are given left most child node of the tree.
How will you get right most child node of the tree.
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 258
Question : Write regular expression which checks for Any occurence of ‘A’ followed by two or more ‘B’ followed
by any occurrence of ‘A’
Question : Merge 2 sorted arrays in constant space and minimum time complexity.
http://www.cs.ubc.ca/~harrison/Java/MergeSortAlgorithm.java.html
http://thomas.baudel.name/Visualisation/VisuTri/inplacestablesort.html
Question: What will happen if in a try block we throw an exception but in the finally block we return a int value?
public class MyFinalTest {
public int doMethod(){
try{
throw new Exception();
}
finally{
return 10;
}
}
public static void main(String[] args) {
MyFinalTest testEx = new MyFinalTest();
int rVal = testEx.doMethod();
System.out.println("The return Val : "+rVal);
}
}
Hint - the method call will return 10 instead of throwing the exception.
Question : How would you write a simple Struts 2 Interceptor which will log the request and response to an
Invocation?
http://www.dzone.com/tutorials/java/struts-2/struts-2-example/struts-2-interceptors-example-1.html
Question : What are different scopes of a Bean in Spring framework?
Answer : singleton – Return a single bean instance per Spring IoC container
prototype – Return a new bean instance each time when requested
request – Return a single bean instance per HTTP request.
session – Return a single bean instance per HTTP session.
globalSession – Return a single bean instance per global HTTP session.
Question : How to create a singleton bean in Spring by calling a custom initialization method (for eg
instance())?
Answer : provide factory method attribute in the bean declaration, as shown below
<bean id="mySingleton" class="org.shunya.MySingleton" factory-method="getInstance" />
4. Racing horses: There are 25 horses in a racing competition. You can have race among 5 horses in a
particular race. What would be the minimum number of races that will be required to determine the 1st, 2nd
and 3rd fastest horses?
5. Two persons: one always speaks truth other always speaks false. You don't know who is what, You are new
to the city, you are allowed to ask exactly one question to find out the direction (for e.g. south or north), what
question would you ask?
6. Three persons: one always speaks the truth, second always lies and third randomly speaks the truth or lies.
You are allowed to ask each entity one or more yes-no questions. You are allowed to ask three such questions.
You must deduce the identities of the three entities with the answers you get. How should you ask the three
questions?
7. Cake cutting: There is a rectangular shaped cake of arbitrary size; we cut a rectangular piece (any size or
orientation) from the original cake. Question is how you would cut the remaining cake into two equal halves in
a straight cut of a knife. And obviously you can’t cut the cake by its cross section.
8. The light bulb problem: You have three light bulbs in a sealed room. You know that initially, all three light
bulbs are off. Outside the room there are three switches with a one-to-one correspondence to the light bulbs.
You may flip the switches however you like and you may enter the room once. How should you flip the
switches to determine which switch controls which light bulb?
9. You have a cylindrical glass with 100% full of water. You have to make it 50% (half). Condition: You are not
supposed to use any scale or any type of measuring instrument.
10. A problem of probability: You are a prisoner sentenced to death. The Emperor offers you a chance to live
by playing a simple game. He gives you 50 black marbles, 50 white marbles and 2 empty bowls. He then says,
"Divide these 100 marbles into these 2 bowls. You can divide them any way you like as long as you use all the
marbles. Then I will blindfold you and shuffle the bowls. You then may choose one bowl randomly and remove
ONE marble from it. If the marble is WHITE you will live, but if the marble is BLACK... you will die." How do you
divide the marbles up so that you have the greatest probability of choosing a WHITE marble?
11. Pirates on deck: Five pirates discover a chest full of 100 gold coins. The pirates are ranked by their years of
service, Pirate 5 having five years of service, Pirate 4 four years, and so on down to Pirate 1 with only one year
of deck scrubbing under his belt. To divide up the loot, they agree on the following:
The most senior pirate will propose a distribution of the booty. All pirates will then vote, including the most
senior pirate, and if at least 50% of the pirates on board accept the proposal, the gold is divided as proposed.
If not, the most senior pirate is forced to walk the plank. Then the process starts over with the next most senior
pirate until a plan is approved. The pirate’s preference is first to remain alive, and next to get as much
gold as possible. The most senior pirate thinks for a moment and then proposes a plan that maximizes his
gold, and which he knows the others will accept. How does he divide up the coins?
What plan would the most senior pirate propose on a boat full of 15 pirates?
12. What is next number in the series:
a> 1, 11, 21, 1211, 111221, 312211...
b> 1, 20, 33, 400, 505, 660, 777,8000, 9009...
13. Sneaking Spider: A rectangular room measures 7.5 meters in length and 3 meters in width. The room has
a height of 3 meters. A spider sits 25 centimeters down from the ceiling at the middle of one of the short walls.
A sleeping fly sits 25 centimeters up from the floor at the middle of the opposite wall. The spider wants to walk
(i.e., move along the walls, floor, and ceiling only) to the fly to catch it. How can the spider reach the fly, walking
just 10 meters? Is it even possible?
14. The Fuse Problem: I have a box of one hour fuses. If I set one end of a fuse on fire, I know that the fuse
will burn all the way to the other end in EXACTLY one hour. However, the fuses may burn unevenly [ie - it may
take 59 minutes to burn the first half of a fuse, but only 1 minute to burn the other half]. Furthermore, all of the
fuses may burn unevenly at a different rate. The only thing we know for sure is that each one takes 1 HOUR to
burn completely. The Question: Given 2 of these fuses and a lighter, how can I time out 45 minutes precisely?
15. Dropping eggs: There is a building of 100 floors If an egg drops from the Nth floor or above it will break If
it’s dropped from any floor below, it will not break You’re given 2 eggs Find N, while minimizing the number of
drops for the worst case.
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 260
16. MIT Mathematicians: Two MIT math grads bump into each other while shopping. They haven't seen each
other in over 20 years.
First grad to the second: "How have you been?"
Second: "Great! I got married and I have three daughters now."
First: "Really? How old are they?"
Second: "Well, the product of their ages is 72, and the sum of their ages is the same as the number on that
building over there..."
First: "Right, ok... Oh wait... Hmm, I still don't know."
Second: "Oh sorry, the oldest one just started to play the piano."
First: "Wonderful! My oldest is the same age!"
How old was the first grad’s daughter?
17. Crazy guy on the plane: A line of 100 airline passengers is waiting to board a plane. They each hold a ticket
to one of the 100 seats on that flight. (For convenience, let's say that the nth passenger in line has a ticket
for the seat number n.) Unfortunately, the first person in line is crazy, and will ignore the seat number on their
ticket, picking a random seat to occupy. All of the other passengers are quite normal, and will go to their proper
seat unless it is already occupied. If it is occupied, they will then find a free seat to sit in, at random. What is
the probability that the last (100th) person to board the plane will sit in their proper seat (#100)?
18. Escape from Alcatraz: A prisoner stays at the maximum security prison on Alcatraz Island. The prison is
in shape of 4X4 cells, the prisoner stays at top right cell with all other cell having a guard, only escape from
prison is from bottom left cell (see diagram for further clarification). Here are the rules for a successful escape
from the prison.
• The prisoner has to escape from the prison overnight by killing all the guards.
• He can only move vertically or horizontally, no diagonal movement is allowed.
• As soon as the prisoner enters a cell he has to kill the guard.
• If he sees the dead guard again he will go mad for 24 hrs out of guilt, i.e he can't go to same cell twice.
Provide an escape route.
19. Transporting bananas: You are standing at point A with 3000 bananas and a faithful camel. Your destination
is point B which is exactly 1000 kms away. The objective is to transport as many bananas as possible to point
B, under the following conditions.
1. Only the camel can carry bananas.
2. The maximum load that the camel can carry at a time is 1000 bananas.
3. The camel consumes 1 banana for every km that it travels. (Irrespective of direction of travel or load)
20. There are 10 marbles of equal weight except for one which weighs a little more. Given a balance how many
weighing are required to deduce the heavier marble. What would be the answer for N marbles? Your answer
should consider the worst case.
21. Imagine a disk spinning like a record player turn table. Half of the disk is black and the other is white.
Assume you have an unlimited number of color sensors. How many sensors would you have to place around
the disk to determine the direction the disk is spinning? Where would they be placed?
22. There are 3 baskets. One of them has apples, one has oranges only and the other has mixture of apples
and oranges. The labels on their baskets always lie. (i.e. if the label says oranges, you are sure that it doesn't
have oranges only, it could be a mixture) The task is to pick one basket and pick only one fruit from it and then
correctly label all the three baskets. How do you do it?
23. Prime pairs: Pairs of primes separated by a single number are called prime pairs. Examples are 17 and 19,
5 and 7 etc. Prove that the number between a prime pair is always divisible by 6 (assuming both numbers in
the pair are greater than 6). Now prove that there are no 'prime triples'.
24. Suicidal Monks: There is a group of monks in a monastery. These monks have all taken a vow of silence.
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 261
They cannot communicate with each other, and all they do is pray in a common room during the day and sleep
at night. As well, they have no mirrors in the compound. One day, the head monk calls them all together and
says "Tonight while you sleep, I will place a black X on some of your foreheads. When you awaken, continue
your normal activities. But once you determine that you have an X, you must wait until night, and then kill
yourself". So from then on, they pray together by day, and each night some may commit suicide. The question:
if there are N monks with Xes, how many days does it take for the N monks to commit suicide?
25. The Monty Hall problem: You are given a choice between three doors -- 1, 2, and 3. One of them contains
a trip to Hawaii, and the other 2 are empty. You pick one. Then he opens one that you didn't pick, and it's
empty. He gives you the chance to switch your choice to the other door you did not choose. Should you change
your original selection?
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 262
How would you find which UNIX operating system you are running?
Following commands can be used to know the OS details -
>uname -a
>arch
How would you find the number of cpu's in your computer?
>cat /etc/cpuinfo
How would you clean a windows file on unix for \n and \r characters?
>dostounix <filename>
Q 246. What is the Typical Interview Coverage for Core Java Candidate?
Java Basics
OOP prinicples, overloading, overriding, exception handling, garbage collection, Immutability, Generics
Collections
New collections introduced in the latest version of JDK, internals of HashMap, ConcurrentHashMap, time
complexity of various collection methods.
Serialization
Custom serilaization using Serializable and Externalizable interfaces. Serializing legacy classes, construction
invocation in serialization.
Data structure and Algorithms
List, Queue, Binary Search Tree, Time complexity of operations, sorting, seraching, etc.
Design Patterns
Singleton, thread-safe siungleton, decorator, adaptor, strategy, builder, factory, observer, etc
Database & Hibernate
Database indexes, types of algorithms for indexes, types of indexes, SQL, SQL tuning, query plan, outer and
inner joins, relationships in database (OneToOne, OneToMany, ManyToMany), inheritance strategies in JPA,
lazy loading, handling concurrency in database transactions.
MVC Framework
MVC design patterns, Interceptors, Dependency Injection, Servlets, Filters, Struts 2, Restful Webservices,
SOA, Spring Framework etc
Misc.
Continuous Integration, Unit Testing, TDD, GC tuning, Maven , UNIX commands, Autosys Jobs, Scripting
Language, etc
In the next 3 pages i have shown my resume as the template, which you can follow if you like !
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 265
Q 249. What are the Interview questions that most candidates answer wrongly?
1. Is it required to synchronize the accessor of a shared mutable object in case of multi-threading? If yes,
why?
2. In what scenario StringBuilder should be preferred over String class?
3. I am working on an application where millions of temporary StringBuilder objects are being created, due to
which application is facing big system wide GC pauses, how would you rectify the problem, assuming that
memory available can not be increased to great extent.
4. How Atomic updates are different from their synchronized counterpart?
5. When do we get the ConcurrentModificationException? What constitutes the structural modifications in a
collection?
6. Is it possible to write a method in Java which can swap two int variable? What if we change the type from
int to Integer?
7. What is difference between Class and the Instance level locking?
8. Can you prove a scenario where thread starvation occurs?
9. Is it a mendate to make all fields final inside a immutable class? If yes, why?
10. How to fix Double Check Locking?
11. What is Java Memory Model? Who should read it?
12. What is upper bound and lower bound in generics?
13. What happens when an exception is thrown from a try block which has no catch clause. But in finally block,
a value is returned by the method? Discuss the scenario.
Chapter - Puzzles Cracking Java Interviews (Java 8, Hibernate & Spring) 266
About Author
Munish Chandel (मुनीश चं देल)