E Book
E Book
Cracking
Java Interviews
( Java 8 )
My skills my Job!!
About Author
Munish Chandel ( )
Munish is Java developer having 9+ years of
experience working for investment banks, consulting
and product companies.
[email protected]
http://linkedIn.com/munish.chandel
Chapter -
Chapter - Introduction
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 Isn't
A research work, neither it is intended to be.
Of much help to a fresher in IT industry as it expects some level of hands on experience. It doesn't even
cover all the topics required by a newbie to start developing software from scratch.
A reference book, one time read should be enough.
This Book Is
Collection of excerpts discussing the common problems faced by an experienced Java Developer in his
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. And there could definitely be more efficient ways to solve the given
problem compared to what is mentioned in this book. The approach shown here is limited to the knowledge
of the author.
Collection of Questions in Core Java 8, Object Oriented Design, Concurrency, Algorithms & Data
Structures, Spring, Hibernate, REST and few puzzles.
Who should read this book?
Experienced candidates who want to brush up their skills for Java Interviews specifically in investment
banking domain (having approach for enterprise level applications) and product based companies.
Experienced Java developers who want to enhance their skills to solve their day to day software problems
in a better way.
I hope this book adds value to your skills. Be a knowledge seeker for lifetime and keep enjoying new learnings!
Munish Chandel
[email protected]
http://linkedIn.com/munish.chandel
August 2014
Chapter - Introduction
Chapter - Introduction
Contents
Cracking Java Interviews
Preface
13
15
Q 1. What are good software practices for developing Scalable, Testable and Maintainable Software ?
15
Q 2. What are good books for reference on Java Programming ?
15
Q 3. What is Growth Road Map for a Java Developer?
16
Q 4. Why should I choose Java for Software Development? What are Pros and Cons of Java 8 ?
17
Q 5. What is difference between 32 bit and 64 bit versions of Java?
17
Q 6. What are four basic principles of OOP?
18
Q 7. What are the key paradigms for Developing the Clean Object Oriented Code?
18
Q 8. What is Role of Requirement Understanding in software development process?
18
Q 9. What is Logarithm? Why is it relevant in Software Development?
20
Q 10. What do you understand by Big O notation, why is it important in software development ?
21
Q 11. How would you determine the Time Complexity of a given algorithm, are there any general guidelines? 22
Q 12. 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'?
23
Q 13. Why Prime Numbers are given much importance in writing certain algorithms like hashcode()?
29
Q 14. What is left shift <<, right shift >> and Unsigned right shift >>> operator in Java? How are these useful?29
Q 15. What is 2's complement notation system for Binary Numbers?
31
Q 16. How Heap space is divided in Java. How does Garbage Collector cleans up the unused Objects ? Why
shouldn't we use System.gc() command in production code?
32
Q 17. What is difference between Stack and Heap area of JVM Memory? What is stored inside a stack and what
goes into heap?
36
Q 18. What is a Binary Tree? Where and why is this used in Java Programs?
37
Q 19. Discuss implementation and uses of TreeSet Collection?
37
Q 20. How does Session handling works in Servlet environment?
38
Q 21. 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 ?
39
Q 22. How will you write a Recursive Program?
40
Q 23. How many elements a complete binary tree could hold for a depth of 10?
40
Q 24. Explain working of a hashing data structure, for example HashMap in Java.
41
Q 25. Discuss internal's of a concurrent hashmap provided by Java Collections Framework.
42
Q 26. 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.
44
Chapter - Introduction
Q 27. Discuss Visitor, Template, Decorator, Strategy, Observer and Facade Design Patterns?
45
Q 28. What is a strong, soft, weak and Phantom reference in Java? Where are these used?
47
Q 29. What are database transaction Isolation levels?
49
Q 30. What is difference between Primary key and Unique Key?
50
Q 31. Why do we need indexing on Database Table Columns ?
50
Q 32. What are clustered and non-clustered indexes in Sybase Database?
51
Q 33. How would you handle lazily loaded entities in web application development using hibernate?
51
Q 34. What are OneToOne, OneToMany and ManyToMany relationship mappings in database design?
52
Q 35. How would you implement ManyToMany mappings with the self entity in JPA?
53
Q 36. What is Inner Join, Left Outer Join and Right Outer Join?
54
Q 37. How will you list all the Customers from Customer Table who have no Order(s) yet?
55
Q 38. How would you fetch Employee with nth highest Age from Employee Table using SQL?
55
Q 39. Question: What is difference between Drop, Truncate and Delete Table commands in SQL?
55
Q 40. What are Inheritance strategies in JPA?
56
Q 41. 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?
56
Q 42. What are different types of Http Codes ?
57
Q 43. What is difference between HTTP Redirect and Forward?
57
Q 44. How will you check the owner information of a given domain name in web ?
58
Q 45. What happens when you type www.google.com in your browser's address bar from an Indian Location?59
Q 46. What is Idiom for Creating a Hibernate Transaction ?
61
Q 47. Why do we need Spring Framework ?
61
Q 48. What is Inversion of Control (or Dependency Injection)?
61
Q 49. What is Bean Factory in Spring?
62
Q 50. What is Application Context?
62
Q 51. What are different types of Dependency Injection that spring support ? or in other words what are the
ways to initialize beans in Spring ?
62
Q 52. What are different Bean Scope in Spring ?
62
Q 53. What are some important Spring Modules ?
62
Q 54. How will you load hierarchy of property files in Spring Context ?
63
Q 55. How to efficiently generate ID's for an Entity in Hibernate/JPA ?
63
Q 56. 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 ?
64
Q 57. How will you handle batch insert in hibernate for optimal usage of memory, network and CPU ?
65
Q 58. How will you operate on records of a large database table with million of entries in it using Hibernate ? 66
Q 59. Do you think Hibernate's SessionFactory and Session objects are thread safe ?
66
Q 60. What is difference between Hibernate's first and second level cache ?
67
Q 61. What is syntax of Cron Expression ?
67
Q 62. Explain Stream API introduced in Java 8 ?
68
Q 63. Most useful Code Snippets in Java 8 ?
70
Q 64. How will you replace tokens from a given text with properties loaded from a property file ?
73
Q 65. How will you configure custom sized ThreadPool for stream parallel operation in Java 8 ?
74
75
75
76
Chapter - Introduction
Q 72. 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 ?
81
Q 73. We are writing an API which will accept a Collection<Integer> as an argument and duplicate an element
in the Original Collection if certain criteria in met. How would you code such an API method ?
82
Q 74. If hashcode() method of an object always returns 0 then what will be the impact on the functionality of
software ?
82
Q 75. Iterator interface provides remove() method but no add() method. What could be the reason for such
behavior?83
Q 76. What does Collections.unmodifiableCollection() do ? Is it a good idea to use it safely in multi-threading
scenario without synchronization, Is it immutable ?
83
Q 77. If we don't override hashcode() while using a object in hashing collection, what will be the impact ?
84
Q 78. How would you detect a DeadLock in a running program ?
84
Q 79. How would you avoid deadlock in a Java Program ?
84
Q 80. Question : How would you produce DeadLock in Java ?
85
Q 81. Which data type would you choose for storing currency values like Trading Price ? What's your opinion
about Float, Double and BigDecimal ?
86
Q 82. How would you round a double value to certain decimal Precision and Scale ?
88
Q 83. How great is the Idea of synchronizing the getter methods of a shared mutable state ? What if we don't ?89
Q 84. Can the keys in Hashing data structure be made Mutable ?
89
Q 85. Is it safe to iterate over collection returned by Collections.synchronizedCollection() method, or should we
synchronize the Iterating code ?
90
Q 86. What are different type of Inner classes in Java ? How to choose a type with example ?
91
Q 87. When should we need a static inner class rather than creating a top level class in Java Program?
91
Q 88. Is it possible to write a method in Java which swaps two int/Integer ?
92
Q 89. What all collections utilizes hashcode() method ?
92
Q 90. Provide a diagram for collections framework.
93
Q 91. What is Immutable Class. Why would you choose it ? How would you make a class immutable ?
94
Q 92. Why shouldn't we prefer mutable static variables in our Java Code ?
95
Q 93. Discuss Exception class hierarchy in Java. When should we extend our custom exception from
RuntimeException or Exception ?
95
Q 94. How does method parameter passing works in Java ? Does it pass-by-reference or pass-by-value ?
96
Q 95. How does an ArrayList expands itself when its maximum capacity is reached ?
96
Q 96. What is StringPool In Java ?
96
Q 97. What is instance level locking and class level locking ?
97
Chapter - Introduction
Chapter - Introduction
125
Concurrency in Java
126
Q 134. What is Concurrency? How will you implement Concurrency in your Java Programs?
126
Q 135. 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?
127
Q 136. What are the different states of a Thread? What does those states tells us?
128
Q 137. Question: What do you understand by Java Memory Model? What is double-checked locking? What is
different about final variables in new JMM?
129
Q 138. Is i++ thread-safe (increment operation on primitive types)?
133
Q 139. What happens when wait() & notify() method are called?
133
Q 140. Discuss about volatile keyword and Java Memory Model?
134
Q 141. What is a CAS? How does it help writing non-blocking scalable applications? Tell something about
Atomic Package provided by Java 1.6
135
Q 142. 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?
136
Q 143. How would you implement AtomicFloat /AtomicDouble using CAS?
137
Q 144. How LongAdder and LongAccumulator are different from AtomicLong & AtomicInteger ?
139
Q 145. Can we implement check & update method (similar to compare and swap) using volatile alone?
139
Q 146. How will you track the largest value monitored by different threads in an non-blocking fashion (using
Atomic Operations) ?
139
Q 147. What is difference between Fork/Join framework and ExecutorService ?
140
Q 148. How does ForkJoinPool helps in writing concurrent applications ? Please provide few examples for
RecursiveTask and RecursiveAction.
140
Q 149. How will you calculate Fibonacci Sequence on a multi-core processor ?
143
Q 150. How will you increment each element of an Integer array, utilizing all the cores of processor ?
144
Q 151. 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?
145
Q 152. 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.
146
Q 153. What are fail-fast Iterator? what is fail safe?
148
Q 154. There is a stream of words which contains Anagrams. How would you print anagrams in a single bucket
from that stream?
149
Q 155. Describe CopyOnWriteArrayList? Where is it used in Java Applications?
150
Q 156. There are M number of Threads who work on N number of shared synchronized resources. How would
you make sure that deadlock does not happen?
150
Q 157. Are there concurrent version for TreeMap and TreeSet in Java Collections Framework?
150
Q 158. Is it safe to iterate over an ArrayList and remove its elements at the same time ? When do we get
ConcurrentModificationException & hidden Iterator?
151
Q 159. What is ThreadLocal class, how does it help writing multi-threading code? any usage with example? 152
Q 160. How would you implement your own Transaction Handler in Core Java, using the EntityManager
created in last question?
153
Q 161. What is AtomicInteger class and how is it different than using volatile or synchronized in a concurrent
environment?154
Q 162. You are writing a server application which converts microsoft word documents into pdf format. Under
Chapter - Introduction
10
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.
155
Q 163. What are common threading issues faced by Java Developers?
157
158
Q 164. G
iven a collection of 1 million integers ranging from 1 to 9, how would you sort them in Big O(n) time?
158
Q 165. 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?159
Q 166. I have an Integer array where every number appears even number of time except one. Find that number.
159
Q 167. how would you check if a number is even or odd using bit wise operator in Java?
160
Q 168. How would you check if the given number is power of 2?
160
Q 169. What is a PriorityQueue? How is it implemented in Java? What are its uses?
161
Q 170. What is difference between Collections.sort() and Arrays.sort()? Which one is better in terms of time
efficiency?162
Q 171. 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.
162
Q 172. 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?
163
Q 173. What is technique to sort data that is too large to bring into memory ?
164
Q 174. Check if a binary tree is a Binary Search Tree or not?
164
Q 175. 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
165
Chapter - Introduction
11
Q 188. How would you multiply a number with 7 using bitwise hacks?
173
Q 189. What is the best way to search an element from a sorted Integer Array? What would be it's time
complexity?173
Q 190. How would you reverse a Singly linked List?
174
Q 191. How would you count word occurrence in a very large file ? How to keep track of top 10 occurring
words?175
Q 192. What is difference between synchronized HashMap and a hashtable?
178
Q 193. What is difference between Iterator and LisIterator?
178
Q 194. What do you understand by Token Bucket Algorithm. What is its use ?
179
Q 195. How will you implement fibonacci series using Iterative & Recursive approach in Java 8 ?
181
Q 196. How will you write a multi-threaded HttpDownloader program using Java 8 ?
184
Q 197. How will you find first non-repeatable character from a String using Java 8 ?
185
Q 198. How will you find Word Frequency in sorted order for a collection of words ?
185
Q 199. How will you calculate MD5 hash of a given String in Java ?
186
What are the key principles when designing a software for performance efficiency ?
How would you describe Producer Consumer problem in Java ?
How would you implement a Caching for HttpDownloader Task using Decorator Design Pattern ?
Write Object Oriented design for library management system.
Design ATM machine.
Design a web crawler that will crawl for links(urls).
Design Phone Book for a mobile using TRIE (also known as prefix tree).
How would you resolve task's inter dependency, just as in maven/ant.
187
187
187
189
190
192
193
194
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]
196
203
Q 208. How would you sort 900 MB of data using 100 MB of RAM ?
What is external sort ?
200
Q 209. How would you design minimum number of platforms so that the buses can be accommodated as per
their schedule ?
Q 210. 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 ?
205
Q 211. 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.
205
Q 212. 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 representing 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.
207
Q 213. How would you simulate a digital Clock in Object Oriented Programming Language?
209
Q 214. How would you design an elevator system for multi story building? Provide with request scheduling
Chapter - Introduction
12
212
212
213
214
Chapter - Introduction
13
Chapter - Introduction
14
money in IT industry is easy and effortless compared to other parallel opportunities. Many people wants to
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.
Question: What is the Problem with Most Indian Developers in terms of Skills ?
Majority of IT crowd does not have good hold over their primary skills (Technical, Presentation) which are
required for the work. The underlying cause for the low skills are poor quality of education and the type of work
which is fed to Indian Companies. The majority of work does not require high quality of skills on developer's
part. Many people learn by their own, build their skills and fight for better quality work. One should have a very
good hold over his primary skill set and look for work which is matching those skills.
Question: What are advantages of acquiring skills ?
1. Very good understanding the basic computer science along with the core language skills helps write very
efficient/scalable/maintainable software that is capable of utilizing the available hardware effectively, with
minimum bugs.
2. Skills alleviates work stress, by empowering us to design intelligently, automating the mundane tasks.
3. When you have good understanding of hardware and software then you get a deep penetration into
software development which otherwise is not possible.
4. Better skills may attract better Job profile.
Question: Would it help if I memorize all the questions for cracking interviews?
No, it will not. But memorizing the most common Patterns of software development will definitely help
crack not only the interview but also make your day to day work life easy. A single pattern resolves n number
of problems emerging from that pattern, and we should always look forward finding the patterns instead of
solution to a particular problem.
Question: Why do interviewers ask rocket science questions in interviews even if the new role does
not require any such skills ?
Hiring in IT industry is not regulated 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.
Question: 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 hamper this.
Question: Quality work never reaches India, is that right ?
Its true for many companies, even in the big MNCs. Seed for existence of Indian counterpart of a MNC is the
cheap and easily scalable labour who is happy to do anything for money. At present, best practices for software
development exists in Europe & some parts of US. Most Indian MNCs are never setup for best talent so we
can not expect quality work at the moment. Work here is mostly committed by management for fixed cost, no
matter how you do it, how long you stretch. The good part is, exceptions are there and changes is in progress.
Chapter - Concepts
15
Chapter 1
Q 1. What are good software practices for developing Scalable, Testable and Maintainable Software ?
1. Understand the requirement and the business, asks questions to resolve ambiguities.
2. 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 very good test coverage (End to End and Unit Tests) keeps a developer away from last minute
stress at production deployment time.
3. Automate all non-productive mundane tasks related to deployment, e.g. Salt Stack or AIDS for Dev Ops
4. Take a software change request only if it is really required. If there is no value addition to the customer then
don't do it.
5. 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.
6. 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.
7. Document interfaces and reasons instead of implementation.
8. 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
9. Use pair programming when bringing someone new up to speed and when tackling particularly tricky
problems which are hard to KT otherwise. This also leads to smooth landing for the new employees.
10. 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)
11. Work in small steps with frequent feedback and correction to avoid the last minute surprises (Agile).
12. Continuous Integration environment is must for rapid bug free, coordinated development. Tools like
TeamCity, Hudson, Jenkins, etc are can be leveraged for Continuous Integration.
13. Software development without Art, Fun and Creativity is boring and will bring depression, so be aware of
this warning sign. Don't leave learning, be a student for lifetime!!!
Framework
6. Spring in Action by Craig
Walls, 4th Edition
7. Java Persistence with
Hibernate
8. Rest In Action, Manning
9. jQuery in Action
Chapter - Concepts
16
Priority
Category
Topics
Practices
Web Tier
Database
Core Java
Algorithms
Data
Structures
Concepts
Chapter - Concepts
17
Q 4. Why should I choose Java for Software Development? What are Pros and Cons
of Java 8 ?
Java Pros
1. Java is free, download it and start creating your own applications
2. Plenty of third party libraries, frameworks & IDE for faster development (spring, hibernate, intelliJ, etc)
3. Platform independent, write once run on most modern platform (Unix, Windows, Mac, 32/64 bit Hardware)
4. Supports Object Oriented Programming, easy to model real life scenarios into object model
5. 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 (Work Stealing Algorithm), non-blocking algorithm using CAS, Stream API, Parallel
Streams, CompletableFuture, Parallel Array Operations, Atomic Values, LongAccumulator, etc.
6. Very good support for Internationalization
7. Memory management is automatic by use of garbage collector (G1, Concurrent Mark Sweep, parallel
scavenge garbage collector, etc)
8. Pure Java Byte code running on 32 bit JVM works perfectly fine on 64 bit platform
9. 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 ay C/C++ written application
2. Normal Java is not good for real time systems because of "stop the world garbage collector pauses".
1 http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html
Chapter - Concepts
18
Q 7. What are the key paradigms for Developing the Clean Object Oriented Code?
1. Program to an Interface (or the Super Type) not the implementation.
2. Interacting Classes should be loosely coupled among themselves.
3. Code should implement tight encapsulation. Use of public and static variables should be avoided
whereever possible, they introduce coupling and make testing of classes tough. Avoid the Singleton Design
pattern whereever possible.
4. Always reuse the code using Inheritance, Composition and Utility Methods. Strictly follow the Do not
Repeat Yourself (DRY) principle.
5. Has-A relationship is better than Is-A relationship because it offer more flexibility, see Decorator Design
Pattern for more details.
6. In case of multi-threaded applications, use immutable objects to represent the state.
7. Make proper use of Design Patterns wherever possible.
8. Use up to date software dependencies & make best use of latest technology available to us.
Chapter - Concepts
19
Chapter - Concepts
20
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.
The Richter scale measures earthquake intensity on a base 10 logarithmic scale.
In astronomy, the apparent magnitude measures the brightness of stars logarithmically, since the eye also
responds logarithmically to brightness.
In Divide and Conquer algorithms (Binary Search, Data Partitioning, etc), the problem set is halved in each
iteration which results in logarithmic Big O (log n) Time Complexity.
http://simple.wikipedia.org/wiki/Logarithm
Chapter - Concepts
21
Q 10. What do you understand by Big O notation, why is it important in software development ?
Big O Notation1 is a mechanism used to measure the relative efficiencies of Algorithms in terms of Space and
Time. It makes us understand how execution time & memory requirements of an algorithm grow as a function
of increasing input size. In this notation, O stands for the Order of magnitude.
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.
Following are the examples of Big O, in increasing order of their magnitude.
Big O Notation
O (1)
O (log n)
O (n)
O (n log n)
O (n2)
O (2n)
O (n!)
Name
Constant-time
Logarithmic
Liner
Log Linear
Quadratic
Exponential
Factorial
Example
Searching from a HashMap, check a number for even/odd
Find an item inside sorted array using Binary Search
Printing all elements from an array
Sorting using Merge Sort
Bubble Sorting Algorithm
Shortest Path Problem Djigstraw Algorithm
Solving Travelling Sales Man Problem
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.
Base of Logarithm is irrelevant in Big O Notation
The base of algorithm is not relevant with respect to the order of growth, since all logarithms with a constant
base are all related by a constant proportion, so log N is used when referring to the order of growth regardless
of the base of Algorithm.
Number -> 1,10,100,1000
Log2 -> 0, 2.3, 4.6, 6.9
Time efficiency in Big O notation for few Java Collections
ArrayList (ignoring the time taken by array resize operation)
O(1) for add, size and get
O(n) for toString() method
PriorityQueue
O(1) for peek, element and size
O(log n) for offer, poll, remove() and add
O(n) for remove(Object) & contains(Object)
HashMap & ConcurrentHashMap (with no collisions)
O(1) for get operation
http://en.wikipedia.org/wiki/Big_O_notation
Chapter - Concepts
22
Q 11. How would you determine the 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.
1. Consecutive Statements (Add the Complexity)
We should add the time complexity of each statement to calculate the total time complexity. For example if we
have 3 lines of code with O(1), O(log n) and O(n) complexity respectively, then the total time complexity would
be O(1)+O(log n)+O(n) = ~O(n)
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)
}
Chapter - Concepts
23
Q 12. What is a sorting algorithm ? List down sorting algorithms by their time &
Requires
Internal Sorting
Internal sorting takes place in the main
memory, utilizing the Random Access
Nature of the main memory. Sufficient
Main Memory should be available for the
input data.
Suitable
Quick Sort, Merge Sort, Heap Sort, Tree
Algorithms
Sort, Selection Sort, Bubble Sort, Insertion
Sort, Counting Sort, etc
Performance Faster due to random access of Main
Memory
Advantage
Faster
Example
Chapter - Concepts
24
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
4. Computation complexity of swaps (In Place algorithms)
Certain algorithms allows in memory swap of elements to perform the sorting thereby offering O (1) space
complexity. Example algorithms that allows In Place sorting are - bubble sort, selection sort, insertion sort,
heap sort and shell sort. Quick sort is kind of in place but requires O (log n) space to keep track of recursive
calls as a part of divide and conquer strategy thus it can not be called In Place algorithm.
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
Worst Time
Complexity
n2
n log n
n log n
n2
n2
Space
Complexity
log n
n
n
1 (In Place)
1 (In Place)
Stable
Quicksort
Binary Tree Sort
Merge Sort
Selection Sort
Bubble Sort
Average Time
Complexity
O (n log n)
O (n log n)
O (n log n)
O (n2)
O (n2)
No
Yes
Yes
No
Yes
Comparison
Based ?
Yes
Yes
Yes
Yes
Yes
Suitable for
Memory
Internal
Internal
External
Internal
Internal
Heap Sort
Insertion Sort
Radix Sort
Counting Sort
O (n log n)
O (n2)
O n.(k/d)
O (n+r)
n log n
n2
n.(k/d)
n+r
1 (In Place)
1 (In Place)
n+2d
n+r
No
Yes
No
Yes
Yes
Yes
No
No
Internal
Internal
External
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.
Sorting Examples in Java 8
1. Sort Array of Strings ignoring the case and print them to System out
public void sortStrings() {
String[] names = {"One", "Two", "Three", "Four", "Five", "Six"};
Stream.of(names).sorted(String::compareToIgnoreCase).forEach(System.out::println);
}
Chapter - Concepts
25
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));
}
Chapter - Concepts
Sorted Output 1
1
2
Java 8 example for Counting Sort
26
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);
}
public int[] sort(int[] input, int min, int max) {
int counting[] = new int[max - min + 1];
//Compute the count of each item
for (int number : input) {
++counting[number - min];
}
//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.
What is Prefix Sum
Prefix sum is the cumulative sum of a sequence of numbers x0, x1, x2 ... is a second sequence of numbers y0,
y1, y2 ..., the sum of prefixes (running totals till a given array position) of the input sequence y0 = x0
y1 = x0 + x1
y2 = x0 + x1 + x2
y2 = x2 + y1
Example of prefix sum for a given input array Input Sequence
1
Prefix Sums (running totals) 1
2
3
3
6
4
10
5
15
Other Variants of Source Code public static void countingSort(int[] array, int min, int max) {
6
21
7
28
Chapter - Concepts
27
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.
A typical Bucket Sort program looks like import java.util.*;
public class BucketSort {
public static void sort(int[] a, int maxVal) {
int[] buckets = new int[maxVal + 1];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = 0;
}
for (int i = 0; i < a.length; i++) {
buckets[a[i]]++;
}
int outPos = 0;
for (int i = 0; i < buckets.length; i++) {
for (int j = 0; j < buckets[i]; j++) {
a[outPos++] = i;
}
Chapter - Concepts
}
28
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
29
Q 13. Why Prime Numbers are given much importance in writing certain algorithms
like hashcode()?
Prime numbers are very useful for generating hashcode, RSA algorithms, random number generators.
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). Prime
numbers are always unique and can not be divided by any other number except 1. 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 14. 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.
Left Shift Operator << (Signed)
00000101
<< by 3
00101000
It shifts the underlying bits of an integer to left by the given distance filling the right most bits with zero always.
X = a << b means the same as X = a*2^b
a is given Number and b is the shift amount.
Here is an example of 8 bit representation of number 5. and when we left shift it's bit by 3 then the right most 3
bits are filled by zero.
And the number becomes
5*23 = 40.
The same thing happens for negative numbers which are represented in 2's complementary notation. for
example -5 becomes -40 as follow
11111011 becomes 11011000
Right Shift Operator >> (Signed)
00101000
>> by 3
00000101
Shifts the bits to left by specified amount maintaining the sign of underlying integer i.e.
It fills the left most bits with 0 if the number is positive otherwise with bit 1.
X = a >> b means same as arithmetic operation X = a / (2b)
Unsigned right shift Operator >>> (does not respect sign of Number, does not preserve the 1st bit)
Chapter - Concepts
30
Unsigned right shift operator >>> is effectively same as >> except that it is unsigned, it fills the left most
11001101
>>> Shift by 1
01100110
positions with bit 0 always. (Irrespective the sign of the underlying number)
For example,
1100 1100 >>> 1 becomes 0110 0110 (shown in diagram)
10000000 >>> 3 becomes 10000 in binary
256 >> 3 becomes 256 / 2^3 = 16.
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.
Chapter - Concepts
31
1 http://en.wikipedia.org/wiki/Two's_complement
Chapter - Concepts
32
Q 16. How Heap space 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.
Java HotSpot VM Heap Memory is divided into Generations1
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.
Eden Space
Survivor 1 & 2
Space
Young Generation
Tenured Space
Tenured Generation
Metaspace
Metaspace
Memory Spaces
Eden Space (heap): The pool from which memory is initially allocated for most objects.
1 http://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html
http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html
continued on 33
Chapter - Concepts
33
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.
Performance Tuning GC2
Set appropriate heap using -Xms and -Xmx VM parameter. Unless you have GC pause problem, you should
give as much memory as possible to the virtual machine.
-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
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
New Ratio = 2 (old generation is 2 times bigger than young generation)
New Size = 2228K
2 http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#generation_sizing.young_gen.survivors
Chapter - Concepts
34
First decide the maximum heap size you can afford to give the virtual machine. Then plot your performance
metric against young generation sizes to find the best setting.
Note that the maximum heap size should always be smaller than the amount of memory installed on
the machine, to avoid excessive page faults and thrashing.
If the total heap size is fixed, increasing the young generation size requires reducing the tenured
generation size. Keep the tenured generation large enough to hold all the live data used by the application
at any given time, plus some amount of slack space (10-20% or more).
Subject to the above constraint on the tenured generation:
Grant plenty of memory to the young generation.
Increase the young generation size as you increase the number of processors, since allocation can be
parallelized.
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.
Sample Settings for increasing Eden Space and New Generation
java -client -XX:SurvivorRatio=12 -XX:NewRatio=2 -XX:NewSize=50m -Xmx256m -Xms64m
-XX:MaxMetaspaceSize=128m -XX:+PrintGCDetails -jar dli-downloader-4.2-jar-with-dependencies.jar
Question: Does GC collects memory from Perm Gen Space ?
Solution : The PermGen space is garbage collected like the other parts of the heap. PermGen contains metadata of classes and objects (pointers to heap memory allocation). It also includes ClassLoaders that need to be
manually destroyed at the end of their use.
Question : What are the available tools to give the visual view of the different memory spaces in a
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.
3 http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#generation_sizing.young_gen.survivors
Chapter - Concepts
You can always profile an application and see the memory trends and customize the memory allocations
accordingly.
35
Chapter - Concepts
36
Q 17. 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.
public static void main(String[] args) {
System.out.println("start");
for (int i = 0; i < 1000 * 1000 * 1000; ++i) {
Foo foo = new Foo();
}
System.out.println(Foo.counter);
}
1 http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html
Chapter - Concepts
37
Q 18. What is a Binary Tree? Where and why is this used in Java Programs?
Binary Tree is a tree data structure made up of nodes. Each node has utmost two children.
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.
Balanced Binary Tree
Binary Tree is useful only when the tree is balanced, because only in that case a Binary Tree provides O(log
n) search complexity, otherwise a binary tree will behave more like a linear data structure with O(n) time
complexity for searching. A tree is called balanced when the height of the tree is logarithmic compared to
number of its elements.
Binary Search Tree
Left child of root is less in value than the right child. And the same is true for left and right sub tree in case of
Binary Search Tree. BST is build for efficiently sorting & searching. In Order Traversal of a Binary Search Tree
results in Ascending Order sorting of its elements.
Binary Tree Implementations used in Java 8
Red-black-tree (TreeMap) and binary heap (PriorityQueue) implementation of Binary Tree is provided by Java
Collection Framework, both of which are thoroughly tested and easy to use.
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.
Chapter - Concepts
38
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.
Chapter - Concepts
39
Q 21. 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 ?
There are two main ways to handle relative Context Path 1. Do not provide absolute context path in your dynamic/static web pages. So instead of mentioning absolute
url (that starts with /), use something like this static/images/a.gif
../static/images/a.gif
<link href="resources/css/style.css" rel="stylesheet" type="text/css" />
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.
<link href="${pageContext.request.contextPath}/resources/css/style.css" rel="stylesheet" type="text/css" />
<img src="<%=request.getContextPath()%>/static/images/a.gif"/>
${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
40
Recursion is helpful in writing complex algorithms in easy to understand manner. But normally iterative
solutions provide better efficiency compared to recursive one because of so much overhead involved in
executing recursive steps.
For example, we would use the following code to calculate the Fibonacci series using recursion
public int fib(int n) {
if (n <= 1)
//Base Condition
return n;
else {
//Recursive case
return fib(n - 1) + fib(n - 2);
}
}
Q 23. 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
B
D
Total Number of nodes in a tree with depth 10 will be = 210 -1 = 1023 nodes
Chapter - Concepts
41
Q 24. 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)
Put Operation - Big O(1) Time Complexity
When we add a key-value pair to hashmap, it queries key's hashcode. Hashmap uses that code to calculate
the bucket index in which to place the key/value. For example, if hashcode is zero then hashmap will place
the key value in 0th bucket. Hashmap strips down the key's hashcode to fit into the existing count of buckets
using a bitwise hack which is equivalent to the shown
bucket 1
bucket 2
Bucket ...
bucket N
below,
bucket index = hashcode % (number of buckets)
index =0
index=1
index = ...
index=N
<entry 1>
<entry 2>
<entry 1>
HASHMAP
<entry 2>
<entry ...>
<entry ...>
<entry N>
<entry N>
Chapter - Concepts
42
In Java 1.8, A ConcurrentHashMap is a hashmap supporting full concurrency of retrieval via volatile reads of
segments and tables without locking, and adjustable expected concurrency for updates. All the operations
in this class are thread-safe, although the retrieval operations does not depend on locking mechanism
(non-blocking). And there is not any support for locking the entire table, in a way that prevents all access.
The allowed concurrency among update operations is guided by the optional concurrencyLevel constructor
argument (default is 16), which is used as a hint for internal sizing.
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.
Here is the code snippet showing HashEntry class
static final class HashEntry<K,V> {
final int hash;
final K key;
volatile V value;
volatile HashEntry<K,V> next;
...
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 : Is this possible for 2 threads to update the ConcurrentHashMap at the same moment ?
Answer : Yes, its possible to have 2 parallel threads writing to the CHM at the same time, infact in the default
implementation of CHM, at most 16 threads can write and read in parallel. But in worst case if the two objects
lie in the same segment, then parallel write would not be possible.
Question : Can multiple threads read from a given Hashtable concurrently ?
Answer : No, get() method of hash table is synchronized (even for synchronized HashMap). So only
one thread can get value from it at any given point in time. Full concurrency for reads is possible only in
Chapter - Concepts
43
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.
Question: What enhancements were made to ConcurrentHashMap in Java 8 ?
Answer: few new methods related to concurrency has been added to CHM in Java 8
1. putIfAbsent (The entire method invocation is performed atomically)
2. compute (The entire method invocation is performed atomically)
3. computeIfAbsent (The entire method invocation is performed atomically)
4. computeIfPresent (The entire method invocation is performed atomically)
5. search (key, value)
6. reduce (key, value)
7. forEach
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 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) ;
Chapter - Concepts
44
The below code snippet will count entries that have value > 100
Q 26. 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.
non-Unicode bytes
Unicode characters
InputStreamReader
Unicode characters
non-Unicode bytes
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 - Concepts
45
Q 27. Discuss Visitor, Template, Decorator, Strategy, Observer and Facade Design
Patterns?
continued on 46
Chapter - Concepts
46
@Override
public void visit(Employee emp) {
System.out.println(emp.getName());
}
@Override
public void accept(EmployeeVisitor visitor){
for (Employee employee : employees) {
visitor.visit(employee);
}
}
Chapter - Concepts
47
Strategy
<<Interface>>
ConcreteStrategyA
ConcreteStrategyB
<<Implementation>>
<<Implementation>>
Q 28. What is a strong, soft, weak and Phantom reference in Java? Where are these
used?
Skills Set - in depth understanding for GC, automatic memory allocation and de-allocation, LRU Cache, etc
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 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
continued on 48
Chapter - Concepts
48
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 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
49
Dirty read
Yes
No
No
No
Unrepeatable read
Yes
Yes
No
No
Phantom read
Yes
Yes
Yes
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.
General practice to choose Isolation level
Choose the lowest isolation level that can keep our data safe.
Chapter - Concepts
50
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>
Chapter - Concepts
51
Q 33. How would you handle lazily loaded entities in web application development
using hibernate?
There are at least two different approaches to handle problem of initializing lazily loaded objects.
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
52
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
53
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 35. 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).
The Inverse side of the relationship can also be described as @ManyToMany(mappedBy="friends")
private Set<User> friendOf = new HashSet<User>();
Chapter - Concepts
54
Q 36. 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.
Explicit Join Notation
SELECT *
FROM employee INNER JOIN department
ON 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 recordeven if no other matching record exists.
LEFT OUTER JOIN
The result of a left outer join (or simply left join) for table L and R always contains all records of the "left" table
(L), even if the join-condition does not find any matching record in the "right" table (R). This means that if the
ON clause matches 0 (zero) records in R (for a given record in L), the join will still return a row in the result (for
that record)but with NULL in each column from R. A left outer join returns all the values from an inner join
plus all values in the left table that do not match to the right table.
SELECT *
FROM employee LEFT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;
References
http://en.wikipedia.org/wiki/Join_(SQL)
Chapter - Concepts
55
Q 37. How will you list all the Customers from Customer Table who have no Order(s)
yet?
We can use Left Join in the SQL query to list all the Customer(s) which have Null Order and put a where
clause to eliminate the rows where Order is not Null, for example
SELECT c.name, c.contact, c.email
FROM
Customer c
LEFT JOIN Orders o ON c.id = o.c_id
WHERE
o.c_id IS NULL;
Though there could be many other ways to fetch the same information from the database.
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)
To find 2nd highest Age, the query would become
SELECT *
FROM Employee E1
WHERE (2-1) = (SELECT COUNT(DISTINCT(E2.Age))
FROM Employee E2
WHERE E2.Age > E1.Age)
Q 39. Question: What is difference between Drop, Truncate and Delete Table 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
56
Q 41. 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.
public class Employee {
@ID int id;
@Version int version;
JPA will issue DML something similar to this command
UPDATE Employee SET ..., version = version + 1 WHERE id = ? AND version = readVersion
1 https://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and
Chapter - Concepts
57
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
@Transactional(readOnly = false, isolation = Isolation.SERIALIZABLE)
public void cleanTaskHistory() { ... }
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.
Forward
1. Control is forwarded to the resource available within the server from where the call is made, the transfer of
control is made internally by the container, where client is completely unaware that a forward is happening.
2. When forward is done, the original request and response objects are transferred along with the additional
parameters if needed.
3. Forward can't transfer control to some other domain.
4. Original URL at the client side remains intact, refreshing the page will cause the whole step to repeat again.
5. Session object is not lost in forward or redirect.
Redirect
1. A redirect is a two step process where web application instructs the browser client to fetch the fetch the
Chapter - Concepts
58
Q 44. 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
Status:CLIENT UPDATE PROHIBITED
Registrant ID:mmr-108695
Registrant Name:Christina Chiou
Chapter - Concepts
59
Q 45. What happens when you type www.google.com in your browser's address bar
from an Indian Location?
Chapter - Concepts
60
Chapter - Concepts
61
Chapter - Concepts
62
Q 51. What are different types of Dependency Injection that spring support ? or in
other words what are the ways to initialize beans in Spring ?
We should constructor injection for all mandatory collaborators and setter injection for all other properties.
Chapter - Concepts
63
Q 54. 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>
<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.
Chapter - Concepts
64
@org.hibernate.annotations.Parameter(value="next_hi", name="column")
})
@GeneratedValue(generator="table-hilo-generator")
@Id
private Long id;
Q 56. 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 ?
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.
For example, below is the file demonstrating the same
@Service
public class AppContextService {
private AppContext appContext;
public AppContext getAppContext() {
return appContext;
}
@PostConstruct
public void loadSettings() throws InstantiationException, IllegalAccessException {
System.out.println("Loading Settings File");
appContext = Utils.load(AppContext.class, "file-searcher.xml");
Runtime.getRuntime().addShutdownHook(new Thread(() -> saveSettings()));
}
//
@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.
public class Main {
public static void main(String[] args) throws IOException {
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "8");
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
FileSearcherApp fileSearcherApp = context.getBean(FileSearcherApp.class);
System.out.println(fileSearcherApp.sayHello());
System.out.println("Press Enter to Exit");
System.in.read();
context.close();
}
}
Chapter - Concepts
65
Q 57. 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
StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
ScrollableResults customers = session.getNamedQuery("GetCustomers")
.scroll(ScrollMode.FORWARD_ONLY);
while (customers.next()) {
Customer customer = (Customer) customers.get(0);
customer.updateStuff(...);
session.update(customer);
}
tx.commit();
session.close();
Chapter - Concepts
66
Q 58. 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();
}
Q 59. 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:
Session sess = factory.openSession();
<begin transaction>
<do your work>
<end transaction>
sess.close();
Chapter - Concepts
67
Q 60. 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 if alive.
Chapter - Concepts
68
Chapter - Concepts
69
Result : sum = 35
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();
stream.filter(Object::nonNull)
Chapter - Concepts
70
There is a magic in above program, we are executing parallel stream operation inside a forkJoinPool, and
number of threads consumed by the parallel stream depends upon the constructor of ForkJoinPool. That
means if ForkJoinPool is created with 6 threads then the concurrency level of parallel stream will be 6.
Copy File using FileChannel Java NIO
public class FileCopy {
public static void fileCopy(File in, File out) throws IOException {
try (FileChannel inChannel = new FileInputStream(in).getChannel();
FileChannel outChannel = new FileOutputStream(out).getChannel();) {
// inChannel.transferTo(0, inChannel.size(), outChannel);
// original -- apparently has trouble copying large
files on Windows
// magic number for Windows, 64Mb - 32Kb)
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size();
long position = 0;
while (position < size) {
position += inChannel.transferTo(position, maxCount, outChannel);
}
}
}
private static void copyFileUsingApacheCommonsIO(File source, File dest)
throws IOException {
FileUtils.copyFile(source, dest);
}
Chapter - Concepts
Sorting on multiple columns ignoring the case using Java 8 Lambda expression
people.sort(
Comparator.comparing(Person::getLastName)
.thenComparing(Person::getFirstName)
.thenComparing(
Person::getEmailAddress,
Comparator.nullsLast(CASE_INSENSITIVE_ORDER)));
71
Chapter - Concepts
Apply 12% VAT on the purchase items and calculate the Bill
List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
double bill = costBeforeTax.stream().map((cost) -> cost + 0.12*cost).reduce((sum, cost) -> sum + cost).get();
System.out.println("Total : " + bill);
72
Chapter - Concepts
73
Q 64. 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;
public class RegexTokenReplacer {
private Pattern pattern = Pattern.compile("\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
public String replaceTokens(String text, Map<String, String> replacements) {
Matcher matcher = pattern.matcher(text);
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
String replacement = replacements.get(matcher.group(1).toLowerCase());
if (replacement != null) {
matcher.appendReplacement(buffer, "");
buffer.append(replacement);
}
}
matcher.appendTail(buffer);
return buffer.toString();
}
public static void main(String[] args) {
RegexTokenReplacer tokenReplacer = new RegexTokenReplacer();
Map<String, String> props = new HashMap<>();
props.put("name", "shunya");
props.put("contact", "+91808066XX");
String replaceTokens = tokenReplacer.replaceTokens("my name is {Name}, {name}'s contact is {contact}",
props);
System.out.println(replaceTokens);
}
}
Chapter - Concepts
74
Q 65. How will you configure custom sized ThreadPool for stream parallel operation
in Java 8 ?
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.
1. Configure Default Common Pool
Its documented that parallel() method utilizes the common pool available per classloader per jvm, and we have
a mechanism to control the configuration of that default common pool using below 3 System properties
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) + " ");
});
75
Chapter 2
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
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));
ConcurrentMap<Person.Sex, List<Person>> byGender =
roster.parallelStream().collect(
Collectors.groupingByConcurrent(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
76
Notes
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
77
class B extends A {
B() {
greeting();
prints();
}
void greeting() {
78
Program Output
Q 69. 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.
79
Class A. Each class has an method add(), is there a way to call A's add() method from
Class C ?
Let's try to create a Class diagram for this scenario.
public class A {
void add() {System.out.println("Add A");}
}
class B extends A {
void add() {System.out.println("Add B");}
}
class C extends B {
void add() {
System.out.println("Add C");
}
80
Q 71. Why wait is always used inside while loop as shown in the below snippet ? Discuss all the probable reasons.
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.
Multiple Threads Waiting for the Single Signals
If a thread calls notifyAll() upon meeting certain condition, then all the consumer threads will wakeup, even
though only one thread will be expected to proceed in that scenario.
Let's analyze problem with the below mentioned Queue's take() method. Suppose there are 2 consumer
threads awaiting for any new item on this shared queue. As soon as the Producer thread will put a single
element into this queue, it will invoke notifyAll() and thus resuming all the 2 Consumer threads. Both the
Consumer threads will come out of waiting state and will fight to acquire lock executing the rest of the code
(line 5-7) one at a time. This will cause the second thread to throw exception because there was just one
element in the queue.
1. public synchronized T take() throws InterruptedException {
2.
if(queue.isEmpty()) {
<=== Problematic If condition
3.
wait();
4.
}
5.
T item = queue.remove();
6.
notify();
7.
return item;
8.
}
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
}
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
81
Q 72. 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.
So lets take code snippet the given method
/***Failing Program, Never call Collection.remove(Object) while iterating***/
package org.shunya.interview;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
public class Test {
public void removeFromCollection(List<Integer> marks) {
for (Integer mark : marks) {
if (mark < 40)
marks.remove(mark);
//==> Will throw java.util.ConcurrentModificationException
}
}
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);
}
82
and duplicate an element in the Original Collection if certain criteria in met. How would
you code such an API method ?
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.
public void addIntoCollection(LinkedList<Integer> marks) {
for (ListIterator<Integer> iterator = marks.listIterator(); iterator.hasNext(); ) {
Integer mark = iterator.next();
if (mark < 40)
iterator.add(mark);
}
System.out.println("marks = " + marks);
}
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 74. 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.
83
Q 75. 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.
84
Q 77. 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
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.
85
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
86
Q 81. 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.
There are two main reasons supporting this statement
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);
}
}
OUTPUT : total = 20.19999999999996
The output should have been 20.20 (20 dollars and 20 cents), but floating point calculation made it 20.1999999999..
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
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
Question : What is Precision and Scale ?
Precision is the total number of digits (or significant digits) of a real number
Scale specifies number of digits after decimal place
For example, 12.345 has precision of 5 and scale of 3
87
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);
}
public static void main(String[] args) {
BigDecimal tempBig = new BigDecimal(22.1214);
System.out.println("tempBig = " + formatRupees(tempBig.doubleValue()));
}
88
Q 82. How would you round a double value to certain decimal Precision and Scale ?
Firstly let us understand the difference between Precision and Scale.
If the number is 9232.129394, then
Precision represents the number of number of significant digits to which a number is calculated i.e. 4 digits
(9232)
Scale represents the number of digits to the right of the decimal point i.e. 6 in above case (129394)
Some other examples are,
Precision 4, scale 2: 99.99
Precision 10, scale 0: 9999999999
Precision 8, scale 3: 99999.999
Precision 5, scale -3: 99999000
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;
public class RoundDouble {
public double round1(double input, int scale) {
BigDecimal bigDecimal = new BigDecimal(input).setScale(scale, RoundingMode.HALF_EVEN);
return bigDecimal.doubleValue();
}
public double round2(double input) {
return Math.round(input * 100) / 100.0d;
}
public double round3(double input) {
DecimalFormat df = new DecimalFormat("#.00");
return Double.parseDouble(df.format(input));
}
The first method of rounding using BigDecimal should be preferred in most scenarios.
89
Q 83. How great is the Idea of synchronizing the getter methods of a shared mutable
state ? What if we don't ?
That's the reason that get() method of vector class is synchronized & must be synchronized.
http://www.ibm.com/developerworks/library/j-jtp08223/
90
Program Output :
before changing key = java.lang.Object@489bb457
after changing key = null
From the above example we see that as soon as we change the key, we are not able to get the associated
object from the Map.
Let's see what's happening inside
When we put the mutableKey to HashMap then
hashcode() is calculated for the key, suppose
it comes out to be 11. So the Object123 is
successfully inserted into the HashMap at bucket
Location 11.
11
22
33
44
Object@123
null
<Empty List>
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.
Q 85. Is it safe to iterate over collection returned by Collections.synchronizedCollection() method, or should we synchronize the Iterating code ?
We should synchronize the code block doing any kind of iteration as stated by the Java Docs
public static <T> Collection<T> synchronizedCollection(Collection<T> c)
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());
}
91
Q 86. 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.
Types of Inner Class Regular Inner Classes (classes defined within the curly braces of a regular class)
Static Inner Classes (that can be accessed without having an instance of outer class)
Method-local Inner Classes (Inner class defined within a method body)
Anonymous Inner Classes (Without any class name)
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 87. 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.
Why to use it ?
1. It is a way of logically grouping the classes that are only used in one place. If a class is useful to only one
other class, then it is logical to embed it in that class and keep the two together.
2. It increases encapsulation.
3. Nested classes can lead to more readable and maintainable code. Nesting small classes within top-level
classes places the code closer to where it is used.
Examples
Iterator in most of the collection types are implemented as a inner class and Entry is implemented as static
inner class.
92
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.
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.
93
List
Iterable
Map
Collection
SortedMap
Set
Queue
SortedSet
Deque
NavigableMap
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.
94
Q 91. What is Immutable Class. Why would you choose it ? How would you make a
class immutable ?
public List<String> getList() {
return Collections.unmodifiableList(list); <=== defensive copy of the mutable field before returning it to caller
}
vi. If a mutable Object is passed in the constructor (like an array), then Immutable class should first make
a defensive copy of the mutable object before storing its reference.
95
Q 92. Why shouldn't we prefer mutable static variables in our Java Code ?
Using mutable static variables might introduce Bugs in your software at some point in time
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 cant be easily unit tested, and statics cant 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 !!
Q 93. Discuss Exception class hierarchy in Java. When should we extend our custom
exception from RuntimeException or Exception ?
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.
Throwable
Exception
IOException
FileNotFoundException
Error
RuntimeException
NullPointerException
ArithmeticException
96
Q 94. How does method parameter passing works in Java ? Does it pass-by-reference 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).
Actually Java always passes a copy of the bits in the variable i.e. Java passes objects as reference and those
references are passed by value. So for a primitive variable, you're passing a copy of the bits representing
the value. For example, if you 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.
And if you're passing an object reference variable, you're passing a copy of the bits representing the reference
to an object. 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.
Q 95. 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.
elementData = Arrays.copyOf(elementData, newCapacity);
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 System.arraycopy(elementData, index, elementData, index + 1, size - index);
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.
97
Example
Lets take this sample example to understand class and instance level Lock.
public class MyClass{
public static synchronized classMethod(){ ... }
public synchronized void instanceMethod(){ ... }
}
private MyClass reference = new MyClass();
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.
98
99
Q 99. What is float-int implicit conversion while doing calculation on mixed data type
in Java?
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 100. 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.
Using Comparator in Java 8 is very easy with Lambda Expression
Arrays.sort(strings, (a, b) -> a.compareTo(b));
Arrays.sort(strings, String::compareToIgnoreCase); // this case using method reference
Arrays.sort(people, Comparator.comparing(Person::getLastName)); // sorts collection of people with lastname
100
Q 101. 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.
101
Q 102. 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
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 103. 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.
Custom TimeZone formatting can be achieved using SimpleDateFormat class.
Calendar instance = Calendar.getInstance();
Date date = instance.getTime();
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss Z");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(formatter.format(date));
formatter.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println(formatter.format(date));
Thus a given time in milliseconds can be represented in different TimeZone using different TimeZone specific
Date formatters.
102
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.
Question: What will be output of the following Java Program ?
Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
Date date = instance.getTime();
System.out.println("date = " + date);
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)
count++;
}
return calendar.getTime();
}
Q 104. Will WeakHashMap's entry be collected if the value contains the only strong
reference to the key ?
103
Q 106. 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
Here is the perfect working example,
public class RemoveViaIterator {
private List<String> names = new ArrayList<>(asList("1st", "2nd", "3rd", "4th"));
public void remove(){
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println("next = " + next);
iterator.remove();
}
System.out.println(names.size());
}
Q 107. Do I need to override object's equals() and hashcode() method for its use in a
TreeMap ?
104
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 which
require both hashcode() and equals() method.
Q 110. 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;
105
import java.util.concurrent.locks.ReentrantLock;
public class BlockingQueue<T> {
private Queue<T> queue = new LinkedList<T>();
private int capacity;
private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
public BlockingQueue(int capacity) {
this.capacity = capacity;
}
public void put(T element) throws InterruptedException {
lock.lock();
try {
while (queue.size() == capacity) {
notFull.await();
}
queue.add(element);
notEmpty.signal();
} finally {
lock.unlock();
}
}
Please note that the thread contention for this class will be slightly less compared to similar implementation
using synchronized keyword, because here we maintain two different Condition Queues instead of just one.
Q 111. 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.*;
106
timedExecution.timedRun(new Runnable() {
@Override
public void run() {
while (!Thread.interrupted()) {
System.out.println("Test me..");
}
}
}, 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 112. 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...}
107
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.
CompletableFuture<Void> links = CompletableFuture.supplyAsync(() -> blockingReadPage(url))
.thenApply(Parser::getLinks)
.thenAccept(System.out::println);
Unlike a plain Future, a CompleteableFuture has a method thenApply to which you can pass the postprocessing function.
108
Q 113. What is difference between intrinsic synchronization and explicit locking using
Lock ?
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.
public interface Lock {
void lock();
void lockInterruptibly() throws InterruptedException;
boolean tryLock();
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
void unlock();
Condition newCondition();
}
Intrinsic synchronization
1 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html
109
Q 114. 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
public class Vector {
private int size;
private Object[] elements;
private StampedLock lock = new StampedLock();
public Object get(int n) {
long stamp = lock.tryOptimisticRead();
Object[] currentElements = elements;
int currentSize = size;
if (!lock.validate(stamp)) { // Someone else had a write lock
stamp = lock.readLock(); // Get a pessimistic lock
110
currentElements = elements;
currentSize = size;
lock.unlockRead(stamp);
}
return n < currentSize ? currentElements[n] : null;
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;
public class BankAccountStampedLock {
private final StampedLock sl = new StampedLock();
private long balance;
public BankAccountStampedLock(long balance) {
this.balance = balance;
}
public void deposit(long amount) {
long stamp = sl.writeLock();
try {
balance += amount;
} finally {
sl.unlockWrite(stamp);
}
}
public void withdraw(long amount) {
long stamp = sl.writeLock();
try {
balance -= amount;
} finally {
sl.unlockWrite(stamp);
}
}
public long getBalance() {
long stamp = sl.readLock();
try {
return balance;
} finally {
sl.unlockRead(stamp);
}
}
public long getBalanceOptimisticRead() {
long stamp = sl.tryOptimisticRead();
long balance = this.balance;
if (!sl.validate(stamp)) {
stamp = sl.readLock();
try {
balance = this.balance;
} finally {
sl.unlockRead(stamp);
}
111
}
return balance;
Q 116. 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;
public class NonRepeatingLetter {
public static void main(String[] args) {
findFirstNonRepeatingLetter(args[0], System.out::println);
}
private static void findFirstNonRepeatingLetter(String s, Consumer<Character> callback) {
s.chars()
.mapToObj(i -> Character.valueOf((char) i))
});
112
Q 118. 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.
113
Object.wait() and Thread.sleep() are entirely two different method used in two different contexts. Here are few
differences between these two methods.
1. wait() method releases the acquired lock when the thread is waiting till someone calls notify() while Thread.
sleep() method keeps the lock even if thread is waiting.
synchronized(monitor) {
Thread.sleep(1000);
// LOCK is held by the current thread
}
synchronized(monitor) {
monitor.wait();
// LOCK is released by current thread
}
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 dont give it another one for at least n milliseconds. The
OS doesnt 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, Dont give it another time slot until someone calls notify(). As with
sleep(), the OS wont even try to schedule your task unless someone calls notify() or one of a few other
wakeup scenarios occurs (spurious wakeup).
114
115
Q 121. 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.
Upper Bounded Wildcards1
Upper bounded wildcard restricts the unknown type to be a specific type or
subtype of that type. For example, If we want to write a method that accepts
List<Number> and its subtypes i.e. List<Double> and List<Integer>, etc then
we can use Upper bounded wildcard. Below is the sample signature of upper
bounded wildcard method.
public static void process(List<? extends Number> list) { /* ... */ }
http://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html
116
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 117
117
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");
}
static void writer() {
f = new FinalFieldExample();
f.names.add("third");
}
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
118
Q 124. 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
String h = "hello" + "world";
to either
String h ="helloworld";
or into
Thus no temporary objects will be created for this type of concatenation. This JVM optimization may improved
further in upcoming releases.
Does that mean, we should never prefer StringBuilder over String ?
No, we never meant that. StringBuilder has its own importance, but in slightly different scenario. Like the
code we discussed above, if the same thing is happening inside a loop then O(n) StringBuilder objects will be
created (one per iteration), causing overall time complexity Big O( n2 ) where n is the number of strings that we
are concatenating. So in that case, for performance reasons, we should create a single StringBuilder Object
outside the loop and then append the data to that StringBuilder Object. That's the reason, experts advice to
use StringBuilder inside a loop. Using StringBuilder, the code should look like this :
StringBuilder result = new StringBuilder(10000);
continued on 119
119
Time Complexity : Big O (n) where n is the number of strings, 1000 in this case.
Q 126. Which Java data type would you choose for storing sensitive information, like
passwords, 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.
120
continued on 121
121
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.
Q 130. Where should we use GET, PUT, POST and DELETE method?
To retrieve a resource, use GET
To create a resource on the server, use POST
To change the state of a resource or to update it on the server, use PUT
To remove or delete a resource on server, use DELETE
5. TreeMap is a SortedMap, based on Red-Black Binary Search Tree which maintains order of its elements
based on given comparator or comparable. Time complexity for put() and get() operation is O (log n).
122
Q 132. 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()) {
123
}
}
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();
}
}
124
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.
public boolean download(String rootUrl, String fileName) throws IOException {
Path path = Paths.get(fileName);
long totalBytesRead = 0L;
HttpURLConnection con = (HttpURLConnection) new URL(rootUrl + fileName).openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(10000);
Above code snippet will transfer bytes from HttpURLConnection's Stream into FileChannel using
transferFrom() method of FileChannel.
Here is snippet of Java Doc for this method
public abstract long transferFrom(java.nio.channels.ReadableByteChannel src,
long position,
long count)
throws java.io.IOException
" 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."
Other features of FileChannel
Bytes may be read or written at an absolute position in a file in a way that does not affect the channel's current
position.
A region of a file may be mapped directly into memory; for large files this is often much more efficient than
invoking the usual read or write methods.
Updates made to a file may be forced out to the underlying storage device, ensuring that data are not lost in
the event of a system crash.
Bytes can be transferred from a file to some other channel, and vice versa, in a way that can be optimized by
many operating systems into a very fast transfer directly to or from the filesystem cache.
A region of a file may be locked against access by other programs.
File channels are safe for use by multiple concurrent threads.
125
Q 133. 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?
There are two main ways to handle such scenario in Java 1.) Use a Socket networking in your application and start a server socket on a predefined port. When second
instance try to start up then check if the port os already occupied or not and accordingly take the decision.
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 - Concurrency
126
Chapter 3
Concurrency in Java
Q 134. 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.
JDK 1.6 provides many useful utility classes in java.util.concurrent package Executors, Queues, TimeUnit, Synchronizers classes (Semaphore, CountDownLatch, CyclicBarrier,
Exchanger), Concurrent collections (ConcurrentHashMap, CopyOnWriteArrayList, ConcurrentSkipListMap),
java.util.concurrent.atomic package for non-blocking algorithms & collections, Lock Interface (ReentrantLock),
and well defined Java Memory Model for memory consistency in concurrent environment.
an example usage of TimeUnit and Lock interface could be to try obtaining lock for 50ms as shown in below
code snippet
Lock lock = ...;
if (lock.tryLock(50L, TimeUnit.MILLISECONDS))
Such a method call can never cause a deadlock scenario because it will try acquiring lock only for 50 ms only.
What is Thread-Safety and how to achieve it ?
A Class is thread safe when it behaves correctly when accessed & modified from multiple threads in parallel
without any changes in the code calling it.
There are certain ways to make our class thread-safe, as follow
1. Use synchronization mechanism (intrinsic or explicit) on the accessor methods, voletile fields, atomic
updates etc.
2. Make the class Immutable and this making it inherently thread-safe.
3. Don't expose the shared state across threads (for e.g. Keep objects local to thread using ThreadLocal)
What is Synchronization ?
Synchronization avoids thread interference and memory consistency errors by providing serialized access to
the shared state.
Synchronization has two major aspects
1. It makes sure that the compound actions executes atomically by providing mutually exclusive access to the
shared state across the threads.
2. It ensures the memory consistency by making the changes visible to all the threads upon method exit.
Lets examine the following program for its correctness in concurrent environment, It maintains the integer
Chapter - Concurrency
127
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 multithreaded 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.
So lets make this program thread-safe.
Counter.java
@ThreadSafe
class Counter {
private int c = 0;
public synchronized void increment() { //this will make the operation execution atomic across the threads
c++;
}
public synchronized int value() {
//this will make sure the changes are visible to the calling thread
return c;
}
}
Q 135. 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 128
Chapter - Concurrency
128
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 136. What are the 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
129
Q 137. Question: What do you understand by Java Memory Model? What is doublechecked 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.
JMM semantics for synchronized
When a thread acquires monitor of an object, by entering into a synchronized block of code, it performs a read
barrier (invalidates the local memory and reads from the heap instead). Similarly exiting from a synchronized
block as part of releasing the associated monitor, it performs a write barrier (flushes changes to the main
memory)
Thus modifications to a shared state using synchronized block by one Thread, is guaranteed to be visible
to subsequent synchronized reads by other threads. This guarantee is provided by JMM in presence of
synchronized code block.
JMM semantics for Volatile fields
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. Moreover afterwards
Java 1.5, volatile reads and writes are not reorderable with any other memory operations (volatile and nonvolatile both). Thus when Thread A writes to a volatile variable V, and afterwards Thread B reads from variable
V, any variable values that were visible to A at the time V was written are guaranteed now to be visible to B.
Let's try to understand the same using the following code
Data data = null;
volatile boolean flag = false;
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){
use data;
}
<-- reading from volatile will perform read barrier for flag as well data.
<--- data is guaranteed to visible even though it is not declared volatile because of the JMM
semantics of volatile flag.
1 http://www.ibm.com/developerworks/library/j-jtp03304/
continued on 130
Chapter - Concurrency
130
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.
Double-Checked Locking Problem
In earlier times (prior to JDK 1.6) a simple uncontended synchronization block was expensive and that lead
many people to write double-checked locking to write lazy initialization code. The double-checked locking
idiom tries to improve performance by avoiding synchronization over the common code path after the helper
is allocated. But the DCL never worked because of the limitations of pervious JMM. This is now fixed by new
JMM (JDK 1.5 onwards) using volatile keyword.
NonThreadSafe Singleton (This will not work under current JMM), so never use it
public class Singleton
{
private Singleton() {}
private static Singleton instance_ = null;
JMM will not guarantee the expected execution of this static singleton.
Chapter - Concurrency
131
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();
}
Chapter - Concurrency
132
}
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.
Using final wrapper to hold the Instance
Semantics of final field in Java 5 can be employed to safely publish the helper object without using volatile.
public class FinalWrapper<T> {
public final T value;
public FinalWrapper(T value) {
this.value = value;
}
}
public class Foo {
private FinalWrapper<Helper> helperWrapper = null;
public Helper getHelper() {
FinalWrapper<Helper> wrapper = helperWrapper;
if (wrapper == null) {
synchronized(this) {
if (helperWrapper == null) {
helperWrapper = new FinalWrapper<Helper>(new Helper());
}
wrapper = helperWrapper;
}
}
return wrapper.value;
}
}
http://www.ibm.com/developerworks/library/j-jtp03304/
http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom#Example_Java_Implementation
http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
http://www.javaworld.com/jw-02-2001/jw-0209-double.html
http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html
http://www.ibm.com/developerworks/library/j-jtp02244/index.html
http://www.ibm.com/developerworks/java/library/j-jtp06197/index.html
http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
http://www.cs.umd.edu/~pugh/java/memoryModel/jsr133.pdf
Chapter - Concurrency
133
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.
How To Make It thread-safe ?
There are mainly two ways to make it thread-safe in Java 1. By making the increment method synchronized. (Preferred when thread contention is moderate to high)
2. By using AtomicInteger to maintain the increment, utilizing CAS under the hood. (Preferred for single CPU,
and for low to moderate thread contention)
If you want, you can write your custom AbstractQueueSynchronizer to achieve the same, but that discussion
is out of scope for this writing.
Q 139. 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.
And in case of notify() method, following things happen
One of the waiting thread (may be a random thread) moves out of the monitor's waiting pool.
Thread comes into ready state (RUNNABLE).
Tries its best to require the monitor lock before it can proceed to the method execution.
Chapter - Concurrency
134
thread-2
-----------if(ready){
//process new content
int var = content.consume();
}
Chapter - Concurrency
135
Q 141. 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.
Memory effects of Atomic Classes
Memory effects for read and update of a atomic variable generally follow the rules for the volatile get() has memory effects of reading a volatile variable.
set() has memory effects of writing a volatile variable.
compareAndSet, getAndIncrement has memory effects of read and write to volatile variable.
Lock-free and wait-free Algorithms using Compare and Swap (CAS)
In a lock-free algorithm, at least some thread always make progress
In a wait-free algorithm, every thread will continue to make some progress in face of arbitrary delay of other
threads.
Below is a small example utilizing CAS for Implementing a non-blocking sequence generator.
class Sequencer {
private final AtomicLong sequenceNumber = new AtomicLong(0);
public long next() {
return sequenceNumber.getAndIncrement();
}
}
We should keep it in mind that CAS operations should be preferred to locking code only when :
1. The operation is very lightweight and confined to a single variable update
2. Thread contention is low to moderate, under heavy contention, performance will suffer dramatically, as the
JVM spends more time dealing with scheduling threads and managing contention and queues of waiting
threads and less time doing real work, like incremental counters.
1 http://www.ibm.com/developerworks/java/library/j-jtp11234/
Chapter - Concurrency
136
Q 142. 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.
public class NonBlockingTradeUpdate {
/***
* This TradeVolume Class must be Immutable otherwise it may create Java Memory Model Problems
* while using with Atomic Reference. All the fields must be final to guarantee the initialization
* and assignment at the same time from other thread.
*/
@Immutable
private static class TradeVolume {
final long quantity;
final long price;
private TradeVolume(long quantity, long price) {
this.quantity = quantity;
this.price = price;
}
}
private final AtomicReference<TradeVolume> tradeVol = new AtomicReference<>(new TradeVolume(100, 200));
public long getQuantity() {
return tradeVol.get().quantity;
}
public long getPrice() {
return tradeVol.get().price;
}
/**
* A non-blocking update method which updates the TradeVolume Object using AtomicReference.
* This method is likely to perform better under multi-core environment with low thread contention.
* @param quantity Quantity of the Trade
* @param price Price of the Trade
*/
public void update(long quantity, long price) {
while (true) {
TradeVolume oldValue = tradeVol.get();
TradeVolume newValue = new TradeVolume(quantity+ oldValue.quantity, price+ oldValue.price);
if (tradeVol.compareAndSet(oldValue, newValue))
return;
}
}
}
1
See Concurrency In Practice chapter 15.3 - Atomic Variable Classes
2 https://www.ibm.com/developerworks/java/library/j-jtp04186/
Chapter - Concurrency
137
The update method can be written concisely using the updateAndGet() method introduced in Java 8
JAVA 8 version of the update method discussed earlier
public void update(long quantity, long price) {
tradeVol.updateAndGet(x -> new TradeVolume(quantity + x.quantity, price + x.price));
}
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 138
Chapter - Concurrency
138
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.
Typical LongAdder Usage would look like final LongAdder adder = new LongAdder();
for (...)
pool.submit(() -> {
while (...) {
...
if (...) adder.increment();
}
});
...
continued on 139
Chapter - Concurrency
139
And LongAccumulator can be used like this LongAccumulator adder = new LongAccumulator(Long::sum, 0);
// from multiple some thread...
adder.accumulate(value);
//final thread
adder.doubleValue();
Q 144. 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.
Q 145. 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
Q 146. How will you track the largest value monitored by different threads in an nonblocking fashion (using Atomic Operations) ?
Wrong Approach
public static AtomicLong largest = new AtomicLong();
// from some other (multiple) thread...
largest.set(Math.max(largest.get(), observed)); // Errorrace condition!
Best Approach In Java 8, we can achieve the same thing in compact code (single line)
largest.accumulateAndGet(observed, Math::max);
//OR
largest.updateAndGet(x -> Math.max(x, observed));
Chapter - Concurrency
140
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.
Q 148. How does ForkJoinPool helps in writing concurrent applications ? Please provide few examples for RecursiveTask and RecursiveAction.
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
141
Diagram in the next page explains the working of RecursiveTask execution in Fork Join Pool (using Divide and
Conquer approach).
Chapter - Concurrency
142
9 10 11 12
split
split
9 10 11 12
10 11 12
Sequential
reduction
Sequential
reduction
Sequential
reduction
Sequential
reduction
Result = a
Result = b
Result = c
Result = d
Merge Result
f = a+b
Merge Result
g = c+d
Final Result
r = f+g
Diagrammatic representation of Recursive Task that sums all elements of array using
Fork Join Pool
Chapter - Concurrency
143
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
144
Q 150. 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;
class IncrementTask extends RecursiveAction {
private final int THRESHOLD = 100;
final long[] array;
final int lo, hi;
IncrementTask(long[] array, int lo, int hi) {
this.array = array;
this.lo = lo;
this.hi = hi;
}
protected void compute() {
if (hi - lo < THRESHOLD) {
for (int i = lo; i < hi; ++i)
array[i]++;
} else {
int mid = (lo + hi) >>> 1;
invokeAll(new IncrementTask(array, lo, mid), new IncrementTask(array, mid, hi));
}
}
Chapter - Concurrency
145
Q 151. 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?
private ConcurrentHashMap<String, BigDecimal> sumByAccount;
this hashmap could contain entries like :
'ICICI Direct' -> 10000.00
'Reliance Money' -> 20000.00
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.
ConcurrentHashMap<String, AtomicReference<BigDecimal>> map;
public void addToSum(String account, BigDecimal amount) {
AtomicReference<BigDecimal> newSum = map.get(account);
for (;;) {
BigDecimal oldVal = newSum.get();
if (newSum.compareAndSet(oldVal, oldVal.add(amount)))
return;
}
}
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
public void addToSum(String account, double amount) {
sumByAccount.computeIfAbsent(account -> new LongAccumulator()).accumulate(amount);
}
Chapter - Concurrency
146
Q 152. 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.
Writing custom synchronizer to address this problem
We will write a custom synchronized class which records the first start time and last stop time.
public class TimeSpread2 {
int threads;
long startTime;
long stopTime;
boolean started = false;
public TimeSpread2(int threads) {this.threads = threads;}
public synchronized void start(){
if(!started){
started = true;
startTime = System.currentTimeMillis();
}
}
public synchronized void stop(){
if(--threads<=0){
stopTime = System.currentTimeMillis();
notifyAll();
}
}
public synchronized long timeSpread() throws InterruptedException {
while(threads >0){ wait(); }
return stopTime-startTime;
}
public static void main(String[] args) throws InterruptedException {
int threads1 = 100;
final TimeSpread2 timeSpread = new TimeSpread2(threads1);
Runnable t = new Runnable(){
public void run() {
timeSpread.start();
try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {}
timeSpread.stop();
}
};
continued on 147
Chapter - Concurrency
147
continued on 148
Chapter - Concurrency
148
public static long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
final CountDownLatch ready = new CountDownLatch(concurrency);
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; i++) {
executor.execute(new Runnable() {
public void run() {
ready.countDown(); // Tell timer we're ready
try {
start.await(); // Wait till peers are ready
action.run();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
done.countDown(); // Tell timer we're done
}
}
});
}
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.
Chapter - Concurrency
149
Q 154. There is a stream of words which contains Anagrams. How would you print
anagrams in a single bucket from that stream?
Sort each word and then see if the two words are equal ? abba, baab, abab should go to the same bucket.
Simple method to check if two Strings are anagrams
public boolean isAnagram(String s1, String s2){
char[] a1 = s1.toCharArray();
char[] a2 = s2.toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
if (Arrays.toString(a1).equals(Arrays.toString(a2))){
return true;
}
return false;
Algorithm
1) Use a hashmap with string as key and list<string> as value where list of strings contain all anagrams of a
given key string.
2) For each word in the input array, create a key by sorting the word and put 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) Print all strings from the list whose key is the input word(sorted string).
Source Code
import java.util.*;
public class Anagrams {
private static Map<String, List<String>> anagramsMap = new HashMap<>(100);
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 time complexity would be N log N (sorting) + N (compare)
Chapter - Concurrency
150
Q 156. There are M number of Threads who work on N number of shared synchronized 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 157. 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.
Concurrent vs Synchronized version
Key Point to note here is that there is difference between synchronized version of TreeMap and Concurrent
version of TreeMap (ConcurrentSkipListMap). Synchronized allows just one thread at a time i.e. access to the
sharedobject is serialized, thus throughput will be low. But Insertion, removal, update, and access operations
can be safely execute concurrently by multiple threads in case of ConcurrentSkipListMap. There is underlying
difference in the implementation of ConcurrentSkipListMap to support this. Same is the case with synchronized
and concurrent version of TreeSet. Thus care must be taken while designing scalable concurrent applications
and preference should be given to concurrent versions.
Time Complexity
Average time complexity is log(n) for the containsKey, get, put, remove ad the variant operations of the
ConcurrentSkipListMap
Chapter - Concurrency
151
Q 158. 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.
Single Threaded Scenario
Never call list.remove(element) to remove a item from list while traversing it. Rather use Iterator.remove()
mehtod.
private List<String> list = new ArrayList<>(asList("first", "second", "third", "fourth"));
public void unsafeMethod() {
for (String item : list) {
// Will throw ConcurrentModificationException
list.remove(item);
}
}
public void safeMethod() {
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
// safe to call iterator.remove()
String item = iterator.next();
iterator.remove();
}
}
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 :
List list = Collections.synchronizedList(new ArrayList(...));
public void safeMethod() {
synchronized(list) {
...rest of the code as shown in previous method
}
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 multithreaded scenario.
Java 8 provides method to conditionally remove items from stream using a filter
Chapter - Concurrency
152
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 159. 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.
ThreadLocal Usage Scenarios
EntityManager in JPA - EntityManager instance is not thread safe but creating too many EntityManagers
could be expensive, thus in a servlet environment ThreadLocal copy could be created using a servlet Filter
and re-used the same EntityManager for the whole request life cycle, committing the changes in the end.
In this case, when the thread calls get() for the first time, a new EntityManager instance is created and the
same is re used on subsequent calls to get() method by the same thread.
public interface SessionCache {
public EntityManager getUnderlyingEntityManager();
}
public class ThreadLocalSession implements SessionCache{
@Override
public EntityManager getUnderlyingEntityManager() {
return threadLocalJPASession.get();
}
private static class ThreadLocalJPASession extends ThreadLocal<EntityManager>{
@Override
protected EntityManager initialValue() {
return JPASessionFactory.getInstance().getSession();
}
}
public static final ThreadLocalJPASession threadLocalJPASession = new ThreadLocalJPASession();
public void set(EntityManager em){
threadLocalJPASession.set(em);
}
public void clear() {
continued on 153
Chapter - Concurrency
153
threadLocalJPASession.get().close();
threadLocalJPASession.remove();
Using Calendar class in multi-threading environment : Calendar.getInstance() is not safe from multithreading 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).
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).
Q 160. 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 very 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.
public interface Transatomatic{
public<T> T run(UnitOfWork<T> unitOfWork);
public static interface UnitOfWork <T>{
public T run();
}
}
public class JPATransatomatic implements Transatomatic {
private final ThreadLocalSession threadLocalSession;
public JPATransatomatic(ThreadLocalSession threadLocalSession){
this.threadLocalSession = threadLocalSession;
}
@Override
public<T> T run(UnitOfWork<T> unitOfWork) {
final EntityManager em = threadLocalSession.getUnderlyingEntityManager();
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
T result = unitOfWork.run();
Chapter - Concurrency
154
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
ThreadLocalSession threadLocalSession = new ThreadLocalSession();
JPATransatomatic transatomatic = new JPATransatomatic(threadLocalSession);
public <T> T find(final Class<T> clazz, final long id) {
return transatomatic.run(new Transatomatic.UnitOfWork<T>() {
@Override
public T run() {
return catalogue.find(clazz, id);
}
});
}
Q 161. What is AtomicInteger class and how is it different than using volatile or synchronized 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.
Here is the implementation for getAndIncrement() method of AtomicInteger Class.
public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}
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
155
Q 162. You are writing a server application which converts microsoft word docu-
ments 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
Continued on 156
Chapter - Concurrency
});
156
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;
public class BoundedHashSet<T> {
private final Set<T> set;
private final Semaphore sem;
public BoundedHashSet(int bound) {
this.set = Collections.synchronizedSet(new HashSet<T>());
sem = new Semaphore(bound);
}
public boolean add(T o) throws InterruptedException {
sem.acquire();
boolean wasAdded = false;
try {
wasAdded = set.add(o);
return wasAdded;
} finally {
if (!wasAdded)
sem.release();
}
}
public boolean remove(Object o) {
boolean wasRemoved = set.remove(o);
if (wasRemoved)
Chapter - Concurrency
157
sem.release();
return wasRemoved;
158
Chapter 4
Q 164. Given a collection of 1 million integers ranging from 1 to 9, how would you
sort them in Big 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
Test Class
Notes
http://en.wikipedia.org/wiki/Bloom%5Ffilter
159
Q 165. 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 thats not possible then
we should created a Trade Wrapper class which overrides these methods.
public class Trade{
...
@Override
public boolean equals(Object o) {...}
@Override
public int hashCode() {...}
}
Q 166. 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.
How does XOR works ?
Here is the complete solution using XORing
public class OddNumberProblem {
private int[] array = {1,1,2,3,4,5,2,3,4};
public int findSingleOdd(){
int result =0;
for (int i : array) {
result=result^i;
}
return result;
}
Output:
singleOdd = 5
160
Q 167. 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.
public static boolean checkOdd(long number){
return ((number & 0x1) == 1);
}
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)
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
1
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.
25
24
23
22
21
161
Q 169. 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.
Implementation - Binary Heap
Binary Heap1 data structure is used
as the underlying for implementing a
PriorityQueue in Java. A Binary MinHeap is a complete binary tree such
that
10
Minimum Value
20
25
40
24
31
35
55
99
Figure : Binary Min-Heap (minimum on top)
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
How is it different from Binary Search Tree ?
Please note that a Binary heap is not a binary search tree.
The ordering in binary heap is top to bottom compared to left to right in case of binary search tree.
Duplicate elements are allowed in Binary Heap which is not the case with Binary Search Tree (a duplicate
key are overwritten by the new key).
A binary heap is a complete binary tree which may not be true for a Binary Search Tree.
Time Complexity for PriorityQueue
Big O(1) time for retrieval methods - peek(), element() and size().
Big O(log n) time for enqueing and dequeing method - offer, poll, remove() and add.
Big O(n) time for remove(Object) and contains(Object) methods.
1 http://en.wikipedia.org/wiki/Binary_heap
162
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 171. 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.
(If sufficient memory is available)
Approach 2
Use BitSet to represent those 1 billion numbers and then traverse the file and set the appropriate Bit in the
BitSet. But check the Bit value before setting it, thus listing the duplicate.
Approach 3
Use bucket Sort to partition the numbers based on some common prefix. Then the duplicate numbers should
fall under the same bucket.
Approach 4
Build a TRIE from this huge file (This will load every thing into memory) and then search the number before
putting it into TRIE.
Some similar questions http://stackoverflow.com/questions/7703049/check-1-billion-cell-phone-numbers-for-duplicates
http://stackoverflow.com/questions/7153659/find-an-integer-not-among-four-billion-given-ones
163
Q 172. 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?
Post-order Traversal
Traverse the left subtree
Traverse the right subtree
Visit the node
Root Node
Pre-order Traversal
Visit the root node
Traverse the left subtree
Traverse the right subtree
In-order Traversal
Traverse the left subtree
Visit the root node
Traverse the right subtree
class.
10
3
Figure : Binary Search Tree Example
In-order
1 http://en.wikipedia.org/wiki/Binary_search_tree
12
164
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 173. 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.
The recursive call makes sure that subtree nodes are within the range of its ancestors. The time complexity will
be O(n) since every node will be examined once.
Further reading - http://cslibrary.stanford.edu/110/BinaryTrees.html#java
Q 175. How would you convert a sorted integer array to height balanced Binary
Search Tree?
165
166
Q 178. How will you swap two numbers using XOR operation?
Swapping int a and b without any temporary variable can be done using XOR operator, as shown below.
a ^= b;
b ^= a;
a ^= b;
This will result in the swap in values of a and b. Please note that there is no fear of value overflow in this case.
167
Q 179. 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.
This can be solved quickly using a customized Quick Sort algorithm.
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 180. Your are give a file with 1 million numbers in it. How would you find the 20 biggest numbers out of this file?
Q 181. Reverse the bits of a number and check if the number is palindrome or not?
168
The following mathematical expression can help us writing a method similar to Math.pow(x, n) using squaring
1 http://cslibrary.stanford.edu/110/BinaryTrees.html#java
169
as a technique for achieving exponentiation. This technique greatly reduces the time complexity to O (log n)
compared to normal way of multiplying the number n times.
1 if n = 0;
1/x-n if n < 0;
x.(xn-1/2)2, if n is odd
(xn/2)2, if n is even
Result
1000
256
Notes
Time Complexity of this algorithm is O (log n) where n is the exponentiation. The number of multiplications
reduces to half on each interation.
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
170
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;
}
}
}
171
Node<T> top;
T pop() {
if (top != null) {
T item = top.data;
top = top.next;
return item;
}
return null;
}
void push(T item) {
Node<T> t = new Node<T>(item);
t.next = top;
top = t;
}
T peek() {
return top.data;
}
172
Q 186. 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.
public class RandomGenerator {
public long generate(){
return System.currentTimeMillis() % 16;
}
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 187. 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.
For upward movement PriorityQueue
The floor number with lower value has the higher priority.
For downward movement PriorityQueue
The floor number with higher value has the higher priority.
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)
173
Q 188. 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.
Multiplying a number with 8 using bit shift operators
If we left shift bits of a number by 23 then it would be equivalent to multiplying the number by 8.
public class MultiplyBy7 {
public static void main(String[] args) {
System.out.println(multiplyBy7(8));
}
Q 189. 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.
Binary Search Algorithm
int low = 0;
int high = list.size()-1;
while (low <= high) {
int mid = (low + high) >>> 1;
Comparable<? super T> midVal = list.get(mid);
int cmp = midVal.compareTo(key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
174
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/
175
Q 191. 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.
We will discuss two approaches to solve this problem Approach 1 : Find the Top K Occurrence count using a hashmap and min-heap (PriorityQueue in Java)
Pseudocode for the algorithm
1. Finding the Word Occurrence Count - Stream the words into a HashMap (put operation is Big O(1))
keeping the value as word occurrence count. On every word occurrence, update the word count.
2. Track Top K occurring Words Using Binary Min Heap (PriorityQueue with Natural ordering) - This can be
word1
word2
word3
word2
word3
Word
Count
word1
100
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.
Java 8 Source Code (Find Top 10 word occurrences)
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.stream.Collectors;
public class SplitWordCount {
public static void main(String[] args) {
List<String> terms = Arrays.asList(
"Coding is great",
"Search Engine are great",
"Google is a nice search engine",
"Bing is also a nice engine");
TopOccurrence topOccurrence = new TopOccurrence(2);
terms.parallelStream() //Utilizes multi-core hardware
.flatMap(s -> Arrays.asList(s.split(" ")).stream())
.collect(Collectors.toConcurrentMap(w -> w.toLowerCase(), w -> 1, Integer::sum)) // Big O(n)
.forEach((s, integer) -> topOccurrence.add(new WordCount(s, integer)));
System.out.println(topOccurrence);
}
static class TopOccurrence {
private final PriorityQueue<WordCount> minHeap;
176
@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
177
0 : null
...
42 : word1
...
60 : word2
...
100 : word1
178
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.
Approach 5 : Another Divide and Conquer approach
1. Read the file and write all words starting with A to file A.txt, words starting with B to file B.txt ... words
starting with Z to file Z.txt.
2. If any of these files are still greater than your memory limit, divide the large files again using their second
letters, i.e. words starting with AA goes into file AA.txt, words starting with AB goes into file AB.txt etc.
3. Since a word cannot appear in two different files now, you can easily count all words at each file and merge
the results without further calculations.
4. You can divide each file in only one pass, so it would take linear time to divide the files. Then, you can
count words in each file and merge the files in linear time.
Useful discussion http://stackoverflow.com/questions/12190326/parsing-one-terabyte-of-text-and-efficiently-counting-the-numberof-occurrences
179
Q 194. 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.
Our TokenBucket Class will have following properties
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.
TokenBucket's consume() method accepts the
number of tokens to consume. This method will then
remove those number of Tokens from the bucket,
refilling the bucket if required. This method utilizes
CAS (CompareAndSet) operation of AtomicLong to
make the resize operation atomic so that no-locking
is required. This will make the class thread-safe
when multiple threads will simultaneously demand
for the tokens.
180
@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);
}
181
@Override
public long getIntervalInMillis() {
return intervalInMillis;
}
Q 195. How will you implement fibonacci series using Iterative & Recursive approach
in Java 8 ?
182
183
this.n = n;
Please note that cache can be employed in this approach too in order to increase the speed of calculation.
Using Java 8 streams to generate Fibonacci in Iterative Approach
static void method1() {
Stream.iterate(new int[]{0, 1},
t -> new int[]{t[1], t[0] + t[1]})
.limit(10)
.map(t -> t[0])
.forEach(System.out::println);
}
static void method2() {
{
IntSupplier fib = new IntSupplier() {
private int previous = 0;
private int current = 1;
public int getAsInt() {
int oldPrevious = this.previous;
int nextValue = this.previous + this.current;
this.previous = this.current;
this.current = nextValue;
return oldPrevious;
}
};
IntStream.generate(fib).limit(10).forEach(System.out::println);
184
Q 196. 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;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;
public class HttpDownloader {
public static void main(String[] args) throws ExecutionException, InterruptedException {
HttpDownloader httpDownloader = new HttpDownloader();
List<String> urls = new ArrayList<>();
urls.add("http://mysite/test1");
urls.add("http://mysite/test2");
urls.add("http://mysite/test3");
urls.add("http://mysite/test4");
httpDownloader.downloadAll(urls);
}
public void downloadAll(List<String> urls) throws ExecutionException, InterruptedException {
AtomicInteger fileCounter = new AtomicInteger(0);
ForkJoinPool pool = new ForkJoinPool(5);
ForkJoinTask<?> task = pool.submit(() -> urls.parallelStream().forEach(url -> download(url, "test-download-" +
fileCounter.incrementAndGet())));
task.get();
pool.shutdown();
}
public void download(String rootUrl, String fileName) {
try {
Path path = Paths.get(fileName);
long totalBytesRead = 0L;
HttpURLConnection con = (HttpURLConnection) new URL(rootUrl).openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(10000);
try (ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
185
}
} catch (IOException e) {
e.printStackTrace();
}
Q 197. How will you find first non-repeatable character from a String using Java 8 ?
package org.shunya.crackingjavainterviews;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import static java.util.function.Function.identity;
public class NonRepeatingLetter {
public static void main(String[] args) {
findFirstNonRepeatingLetter(args[0], System.out::println);
}
Q 198. How will you find Word Frequency in sorted order for a collection of words ?
public static void wordsByFreqSorted(){
List<String> keywords = Arrays.asList("Apple", "Ananas", "Mango", "Banana", "Beer","Apple","Mango","Mango");
Map<String, List<String>> result = keywords.stream().sorted()
.collect(Collectors.groupingBy(it -> it.toString()));
System.out.println(result);
}
Q 199. How will you calculate MD5 hash of a given String in Java ?
Java provides MessageDigest class for calculation of MD5, SHA, etc.
MD5 calculation in Java
public static String getMD5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String hashtext = number.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
186
Chapter - OO Design
187
Chapter 5
Q 200. What are the key principles when designing a software for performance efficiency ?
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.
Chapter - OO Design
import java.util.logging.Level;
import java.util.logging.Logger;
public class ProducerConsumerProblem {
public static Object exit = new Object();
public static void main(String args[]) {
BlockingQueue sharedBlockingQueue = new LinkedBlockingQueue();
Thread producerThread = new Thread(new Producer(sharedBlockingQueue));
Thread consumerThread = new Thread(new Consumer(sharedBlockingQueue));
producerThread.start();
consumerThread.start();
}
}
class Producer implements Runnable {
private final BlockingQueue sharedQueue;
public Producer(BlockingQueue sharedQueue) {
this.sharedQueue = sharedQueue;
}
public void run() {
int i = 0;
while (i < 10) {
try { System.out.println("Produced: " + i);
sharedQueue.put(i);
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
try { sharedQueue.put(ProducerConsumerProblem.exit); } catch (InterruptedException e) {}
}
}
class Consumer implements Runnable {
private final BlockingQueue sharedQueue;
public Consumer(BlockingQueue sharedQueue) {
this.sharedQueue = sharedQueue;
}
@Override
public void run() {
while (true) {
try {
Object item = sharedQueue.take();
System.out.println("Consumed: " + item);
if(item == ProducerConsumerProblem.exit)
break;
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
188
Chapter - OO Design
189
Q 202. How would you implement a Caching for HttpDownloader Task using Decorator 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.
Class Diagram for CachedHttpDownloader using Decorator Design Pattern
Chapter - OO Design
190
}
return path.toFile();
@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.
Question : Why didn't we choose CachedHttpDownloader to extend from RealHttpDownloader ?
We could have chosen another approach of creating the CachedHttpDownloader by extending it from
RealHttpDownloader class, but that could have limited our implementation from the benefit of extending it from
any other class. Java does not allow a class to extend from more than one class.
Design
Journal and Book are specific type of Publication so we can map them into Object Oriented World by making
Publication Class as Abstract and then Book, Magazine and Journal extending the Publication.
Similarly borrow, return and renew are the type of transaction that a library user will typically be performing.
Chapter - OO Design
Transaction can be made an interface and Return, Borrow & Renew will implement this interface.
For further reading Chapter 6. Object oriented design with UML and Java
Here is the class diagram for Library Management System.
191
Chapter - OO Design
192
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.
Transaction -> Balance Inquiry, Withdrawal, Deposit (using generalization)
Prompt user if the user requires receipt or not and accordingly execute the action.
Chapter - OO Design
193
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.
For further reading
http://www.harding.edu/fmccown/classes/comp475-s09/WebCrawler.java.txt
Chapter - OO Design
194
Q 206. 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
\0
storing dictionary like data. Phone book can
be perfectly implemented using TRIE which
will save memory as well as time for prefix
A B C D E ... Z
searching.
A typical Node of a Trie is defined as
static class TrieNode {
char letter;
TrieNode[] links;
boolean fullWord;
TrieNode(char letter) {
this.letter = letter;
links = new TrieNode[26];
this.fullWord = false;
}
}
L T A D E
L
A
T
...
E
N
Chapter - OO Design
195
http://en.wikipedia.org/wiki/Trie
http://en.literateprograms.org/Suffix_tree_%28Java%29
http://code.google.com/p/google-collections/source/browse/trunk/src/com/google/common/collect/PrefixTrie.java?r=2
Chapter - OO Design
196
Q 207. How would you resolve task's inter dependency, just as in maven/ant.
Let's consider the following task dependencies.
Task
3
2
4
5
Dependent On
1,5
5,3
3
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
3
1
Chapter - OO Design
197
Chapter - OO Design
198
Chapter - OO Design
199
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.
1.
1.
2.
3.
4.
5.
Chapter - OO Design
200
Q 208. How would you sort 900 MB of data using 100 MB of RAM ?
What is external sort ?
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.
Lets try to understand this with a concrete example,
Imagine you have numbers 1-9,
{9 7 2 6 3 4 8 5 1}
And lets suppose that only 3 fit in the main memory.
So break the data into 3 chunks, sort each, store in separate files. The contents of 3 files will now become
279
346
158
Now you would open each of 3 files as streams and read the first value from each.
231
Output the lowest value 1, and get the next value from that stream, now you have
235
output the next lowest value , 2 and continue onwards until you have outputted the entire sorted list.
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)
Java Code Example For External Merge Sort
1
2
http://en.wikipedia.org/wiki/External_sorting#External_merge_sort
http://en.wikipedia.org/wiki/Merge_sort
Chapter - OO Design
201
Chapter - OO Design
202
Chapter - OO Design
203
Q 209. How would you design minimum number of platforms so that the buses can
be accommodated as per their schedule ?
BUS
Arrival Time (HRS)
A
0900
B
0915
C
1030
D
1045
E
1100
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.
Algorithm Skills Required : Sorting, Finding Max from an Array.
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
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
204
Chapter - OO Design
205
Q 210. 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, 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 - 2 different fields to act upon.
Lets see how we can solve this problem using Java.
HashSet is a good option for removing duplicates from the collection, so iterate over the entire collection of
feeds and then add them to HashSet, rest will be taken care by HashSet. But we need to override equals and
hashcode based on Tic# so as to remove the duplicate TIC# entries. So our first requirement of removing
duplicates will be done after implementing this.
Now for finding top 10 feeds based on prices, we can use PriorityQueue (min heap) of fixed size 10. While
iterating over the HashSet entries we will check if the price of the feed is greater than the peek entry of
PriorityQueue, if yes then poll the entry and offer the new one. This way we will get a list of top 10 priced feeds.
Print device for printing Top N numbers from a collection.
Make the PriorityQueue's size configurable so that it can be adjusted as per the requirement.
Q 211. 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
Chapter - OO Design
206
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.
public class ParkingManager {
private Queue<Slot> slots = new PriorityQueue<>();
private HashMap<Vehicle, Slot> parkDetail = new HashMap<Vehicle, Slot>();
public Slot getVehicle(String id) {...} //Logic for searching a vehicle using vehicle Id or slot id.
public Slot park(Vehicle vehicle) {...} //save the <vehicle - slot> mapping inside a hash table
}
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
Chapter - OO Design
207
Q 212. 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 representing 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();
}
Chapter - OO Design
}
Notes
Class Diagram For the Solution
208
Chapter - OO Design
209
Q 213. 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.
Let's now see the very basic implementation of this clock.
Chapter - OO Design
@Override
public void display(){
System.out.println("seconds = " + count);
}
210
Chapter - OO Design
if(count>=60){
count=0;
observer.increment();
}
@Override
public void display() {
System.out.println("minutes = " + count);
}
@Override
public void display() {
System.out.println("hours = " + count);
}
void display();
211
Chapter - OO Design
212
Q 214. 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.
For further reading
http://thought-works.blogspot.in/2012/11/object-oriented-design-for-elevator-in.html
Q 215. 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?
Hashing technique could be utilized to solve this problem.
Pseudo Code
for 1st file
read each line,
hash into their abc..xyz buckets depending on the start of the letter of the word. (26 buckets for A to Z to form
something like a 26 by xxx table)
then sort each 26 rows in the hash table and delete duplicates
for 2nd file
sort and delete duplicates
for each line/name, find match in the hashtable created earlier.
if match found, output to another file name.
B+ sorting is surely another possible solution we can look for.
http://en.wikipedia.org/wiki/B%2B_tree
Chapter - OO Design
213
Q 216. 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 - Puzzles
214
Chapter 6
Chapter - Puzzles
215
Chapter - Puzzles
216
Q 220. What is probability of a daat, hitting closer to centre of a circle rather than circumference ?
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.
Probability of hitting the daat closer to circle
(R)2
(R/2)2
=1/4
=25%
Q 221. 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
217
Chapter - Puzzles
218
Chapter - Puzzles
219
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 220
Chapter - Puzzles
220
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 doesnt 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 221
Chapter - Puzzles
221
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 222
Chapter - Puzzles
222
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-comparisom
http://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-lengthn-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 shouldnt 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
223
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" />
Chapter - Puzzles
224
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 cant 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 pirates 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
its dropped from any floor below, it will not break Youre given 2 eggs Find N, while minimizing the number of
drops for the worst case.
Chapter - Puzzles
225
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 grads 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.
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
226
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
227
Chapter - Puzzles
228
1. What do you understand by thread-safety ? Why is it required ? And finally, how to achieve thread-safety in
Java Applications ?
Hint : discuss the need for the concurrent programming, using volatile, synchronization, Immutability &
Atomic packages to address the concurrency problems. Discuss the Java Memory Model. Impact of final
keyword in Java. Differences between wait and notify method in Object class.
2. What are the drawbacks of not synchronizing the getters of an shared mutable object ?
3. Discuss the Singleton Design Pattern ? How to make it thread-safe ? Discuss the Double Check Locking?
4. Can Keys in HashMap be made Mutable ? What would be the impact in that case ?
5. How would you implement your own ThreadPool in Java ? Discuss the designing part.
6. How would you implement a Stack or a Queue in Java ? It must be synchronized.
7. Discuss Big O notation for calculating relative performance of Algorithms. How do various collection
methods perform in terms of Big O Notation ?
8. Implement Queue using an ArrayList.
9. What are the types of Inner classes with example of each ?
10. What is a tree map ? Discuss its underlying implementation i.e. red-black binary tree.
11. There are 1 million trades, you need to check if a given trade exists in those trades or not. Which Collection
would you chose to store those 1 million trades and why ?
Hint : think from time complexity point of view and why HashSet could be a better data structure for storing
these trades assuming we have sufficient memory to hold those items.
12. What is difference between StringBuilder and String ? Which one should be preferred.
13. In a program, multiple threads are creating thousands of large temporary StringBuilder objects. Life span
of all those objects is 1 GC cycle. But somehow we are getting JVM pauses in our application. How would
you troubleshoot the problem ?
Hint : Think from GC tuning perspective, setting the appropriate survivor ratio for proper eden space.
14. What are memory generations in Hot Spot VM ? How generational GC's work ?
15. What is difference between Primary Key and Unique Key ?
16. What is clustered and non-clustered index ?
17. What is Outer and Inner Join ?
18. What is ADT ? We do not need to know how a data type is implemented in order to be able to use it.
19. Are you familiar with a messaging system i.e. MQ ? What is a QueueManager ? Why do you think the
Queue is so important in banking world ?
20. How would you make an application asynchronous ? Can Message Queues help achieving this ?
21. How to achieve loose coupling in your application ?
22. What is TDD and how it helps Agile methodology of software development ?
23. How to make a class Immutable ? What purpose Immutablity solve ?
24. What is difference between Callable and Runnable ?
25. What are Inheritance strategies in JPA ?
26. Discuss Internals of HashMap and ConcurrentHashMap ?
27. What is best way to store Currency Values in Java application ?
28. What is AtomicInteger and how it is useful in concurrent environment ?
29. What are key principles while designing Scalable Software Applications ?
30. What does Collections.unmodifiableCollection() do ? is it useful in multi-threading environment ?
31. How would you add an element to a Collection while iterating over it in a loop ?
32. There are 3 Classes A, B and C. C extends B and B extends A, each class has a method named add() with
same signature (overriding). Is it possible to call A's add() method from Class C ? Reason ?
33. How would you write a simple implementation for Struts 2 Interceptor which just logs the request and
response of an Action ?
Chapter - Puzzles
229
Q 227. 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
Chapter - Puzzles
230
Q 230. 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
231
About Author
Munish Chandel ( )
Munish is Java developer having 9+ years of experience working
for investment banks, consulting and product companies. As of
this writing, he was working for a MNC in Healthcare domain.
By academics, he holds a degree in mechanical engineering
from NIT Hamirpur in year 2005. His hobbies include trekking,
biking, photography, exploring ancient Hindu Texts ( - ,
) , Ayurveda (), yog-asana (), Astrology
and Developing Java Applications, etc.
Author contributes to the below mentioned Open Source
Projects in Java DLI Downloader for Digital Library of India (https://github.com/cancerian0684/dli-downloader)
Punter for Dev Assistance (https://github.com/cancerian0684/Punter)
AIDS - Autonomous Integrated Deployment Software (https://github.com/Buddh/AIDS)
Munish can be reached at [email protected]
http://linkedIn.com/munish.chandel
https://www.facebook.com/munish.chandel