Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

2 Scenario Based Data Structure and Algorithm Design Interview Questi... https://javarevisited.blogspot.com/2017/03/2-practical-data-structure-algo...

Javarevisited
Blog about Java, Programming, Spring, Hibernate, Interview Questions, Books and Online Course Recommendations from Udemy, Pluralsight, Coursera, etc

Home cor e java spr ing hiber nate collections multithr eading design patter ns inter view questions coding data str uctur e OOP java 8 books About M e

Java Cer tifications JDBC jsp-ser vlet JSON SQL Linux Cour ses online r esour ces jvm-inter nals REST Eclipse jQuer y Java IO Java XM L

T UE SDA Y , M A R C H 2 8 , 2 0 1 7

2 Scenario Based Data Structure and Algorithm Design Interview Questions from
Investment Banks

No doubt that Data structure and algorithms are


an integral part of any Programming job
interview, including Java, C++ or any other
programming language. In fact Data structure
and algorithms are quite a favorite one and all
top notch companies including Google,
Microsoft, Amazon and investment banks like
Goldman Sachs, Citigroup, Barclays, Morgan Follow by Email
Stanley or J.P. Morgan focus extensively on data
structure and algorithms, while hiring both Email address... Submit

senior and mid-level developers in Java, C++,


and C# positions. When I shared some
traditional, popular, and more frequently asked questions on Data structure and Algorithms in
my earlier article, I received a lot of feedback to share some practical, scenario-based
questions on data structure e.g. which kind of data structure will you use in a particular
scenario and why.

This sounds me really interesting, because this not only test your knowledge of existing data
structure e.g. array, linked list, trees, graphs, stack, queue, ring buffer, Map but also test how
well you can apply and use this data structure in a given situation, and in some cases, can
you come up with a new innovative data structure.

Of course, classical questions on data structure e.g. finding middle element of linked list in
one pass or internal working of HashMap still matters, but these new questions will give you
more fun and edge while preparing for Java, C++ or C# developer roles.

Inter v iew Questions

cor e java inter view question (169)


Coding Inter view Question (72)
data str uctur e and algor ithm (70)
inter view questions (48)
2 Scenario Based Data Structure and Algorithm Interview object or iented pr ogr amming (31)
Questions SQL Inter view Questions (30)
design patter ns (30)
introduction, let's see some good, practical questions from data
thr ead inter view questions (30)
collections inter view questions (25)
spr ing inter view questions (19)
ture will you use to implement a Limit Order Book? and Why?
database inter view questions (16)
ed mostly on investment banking Java interviews. Sometimes it's also
ser vlet inter view questions (15)
ented design question, which involved detailed design and coding. Let
Pr ogr amming inter view question (6)
of a Limit order book, especially for those programmers who are not from

1 of 6 8/19/2019, 6:58 PM
2 Scenario Based Data Structure and Algorithm Design Interview Questi... https://javarevisited.blogspot.com/2017/03/2-practical-data-structure-algo...

financial services firms or not familiar with stock trading. hiber nate inter view questions (6)

In Stock trading, exchanges like NYSE (NewYork Stock Exchange) maintains an order book for Best of Jav ar ev isited
every security or stock which is traded on their exchange e.g. GOOG, which is a symbol of How Spr ing MVC w or ks inter nally?
Google's stock. There are mainly two kinds of orders customers can send, a buy order, and a How to design a vending machine in Java?
sell order. When we use Limit Price, which means buy order with a limit price of $50 can be How HashMap w or ks in Java?
executed if it found a sell order of $50 or an order of lower price says $49, but it can not be Why Str ing is Immutable in Java?
executed with a sell order of price $51. Similarly, a sell order can execute for a price, which is 10 Ar ticles Ever y Pr ogr ammer Must Read
either equal to or higher than limit price. In general, a LIMIT order executes if it found a How to conver t lambda ex pr ession to method
specified price or better price(lower in the case of a buy, and higher in the case of sell). r efer ence in Java 8?
10 Tips to impr ove Pr ogr amming Skill
10 OOP design pr inciples pr ogr ammer should know
Orders are executed in first come first serve basis, so exchange also maintains a time priority. How Synchr onization w or ks in Java?
Now, let's see the flow, an order comes to exchange, exchange looks order book for that 10 tips to w or k fast in Linux
symbol, if it found a match it executes order otherwise, it adds that order at the end of a price 5 Books to impr ove Coding Skills
queue, which represents time priority, head of the queue represent the order with highest time
priority i.e. the order which comes before all the order below it. Jav a Tutor ials

date and time tutor ial (21)

Now our goal is to perform above operation as quickly as possible. If you look at closely, it FIX pr otocol tutor ial (15)

involves finding opposite order of matching the price, which is equal to or less/greater than Java Cer tification OCPJP SCJP (25)
specified price, removing the order from order book if it matched or canceled, adding order java collection tutor ial (73)
into order book at an appropriate place if not matched. java IO tutor ial (28)
Java JSON tutor ial (12)
Java multithr eading Tutor ials (56)
Java Pr ogr amming Tutor ials (18)
Java x ml tutor ial (16)
JDBC (29)
jsp-ser vlet (37)
online r esour ces (136)

Follower s

Follow ers (4762) Next

In all these operations, traversing is a key, which hints towards binary search tree data
structure. If we use a Binary Tree for storing order, we can find a matching order using binary Follow
search which is of order O(log2N), not quite fast as O(1) but still decent one. Subscr ibe to Download the E-book

Similarly adding and removing orders will also cost that much time, because they involve
traversal. Now in order to tackle different symbol, since the order of one symbol can not match
to order of another symbol, an OrderBook must be associated with a symbol. This is just a
basic idea, without going into exact requirement i.e. methods required to be supported by an
order book.

Btw, If you are not familiar with tree data structure and its variants e.g. binary search tree,
balanced tree-like AVL and Red-Black tree and Tries then I suggest you first read a good book
on Data Structure and Algorithms e.g. Introduction to Algorithms by Thomas H. Cormen

Do w nlo ad Building a REST API w ith


The E-book Spring 4?

Email address... Submit

Categ or ies

cour ses (81)


SQL (55)
linux (38)
database (34)

2 of 6 8/19/2019, 6:58 PM
2 Scenario Based Data Structure and Algorithm Design Interview Questi... https://javarevisited.blogspot.com/2017/03/2-practical-data-structure-algo...

Eclipse (28)
Java Cer tification OCPJP SCJP (25)
JVM Inter nals (23)
JQuer y (18)
REST (17)
Maven (12)
Testing (11)
gener al (11)

Blog A r chiv e

► 2019 (211)
► 2018 (54)
▼ 2017 (781)
► December (90)
► November (31)
► October (44)
So in short, you can use something like this:
► September (40)
► August (51)
► July (55)
class MathcingEngine{
► June (54)

private Map books; ► May (47)


► Apr il (79)

} ▼ Mar ch (99)
How to use EnumSet in Java w ith Ex ample

class OrderBook{ Failed to connect to queue manager - WebSpher e


MQ ...
private BinaryTree<Order> orders;
Pr ivate in Java: Why should you alw ays keep
fields...
public execute(Order ord){
What is Effectively Final var iable of Java 8
//find matching order
How to Compar e tw o Str ing in Java - Str ing
//if match found then create trade
Compar i...
//else add this order into tree
How to for mat Date in SQL Ser ver and Sybase
} Ex ampl...
}
Hiber nate NullPointer Ex ception due to Space in
HQL...
How to loop Ar r ayList in Java - List Itr ator
I have used Queue data structure to maintain time priority for orders of the same price. Of Tr ave...
course, this is very high level, but this does give you some idea on how to approach a What is Asser tion in Java - Java Asser tion
problem and choose a particular data structure. Remember choice of data structure is mainly Tutor ia...
2 Scenar io Based Data Str uctur e and Algor ithm
driven by the operation performed on that e.g. We used Queue instead of Stack here, because
Desi...
the order which comes first, should execute first (FIFO) if price matches.
How to Conver t and Pr int Byte ar r ay to Hex
Str ing ...
I have used a tree data structure because we need to find either specified price or a better Par sing Lar ge JSON Files using Jackson
price, which involves search between different price. Remember, we have not yet considered Str eaming A...

concurrency, thread-safety and all those things which are quite important while designing 3 w ays to Find Fir st Non Repeated Char acter in
a S...
structures for high volume, low latency application, but that's a topic for separate discussion.
Differ ence betw een Stack and Queue Data
Str uctur e ...
Here our goal is to focus on choosing right data-structure. Also, if you think the binary tree is
How to Rever se Ar r ay in Place in Java? Solution
not a right data structure for representing a limit order book, you can definitely share your Wi...
thoughts and solution with us. We will see if it solves the problem with better performance in Differ ence betw een WeakRefer ence vs
terms of time and development effort. SoftRefer ence ...
Java 1.5 Gener ics Tutor ial: How Gener ics in Java
w ...
On the other hand, if you are not familiar with essential data structures like the array, linked
10 Ex ception handling Best Pr actices in Java
list, binary search tree, balanced trees like AVL and Red-Black Tree and hash tables, I Pr ogr ...
suggest you read a good book on data structures. For Java developers, I recommend reading Differ ence betw een star t and r un method in
Algorithms Made Easy" by Narasimha Karumanchi. Thr ead ...
Differ ence betw een Wait and Sleep, Yield in Java
Top 10 Ser vlet Inter view Question Answ er s -
J2EE
Differ ence betw een Stack and Heap memor y in
Java
Why use Pr epar edStatement in Java JDBC –
Ex ample T...

3 of 6 8/19/2019, 6:58 PM
2 Scenario Based Data Structure and Algorithm Design Interview Questi... https://javarevisited.blogspot.com/2017/03/2-practical-data-structure-algo...

SimpleDateFor mat in Java is not Thr ead-Safe Use


Ca...
How to find file and dir ector y size in Unix w ith
E...
How Gar bage Collection w or ks in Java
10 differ ence betw een Java and JavaScr ipt for
Pr og...
Differ ence betw een tr ansient and volatile
keyw or d ...
How to fix java.io.FileNotFoundEx ception:
(Access ...
Top 10 basic netw or king commands in linux /unix
How to Rever se an Ar r ay in Java - Integer and
Str i...
Top 30 Scala and Functional Pr ogr amming
Inter view ...
Binar y Sear ch vs Contains Per for mance in Java
List...
How to check if tw o Str ing ar e Anagr am in Java
- P...
2) Which data structure will you choose to store Market data? and Why?
How to Gener ate MD5 Hash in Java - Str ing Byte
This is another data structure questions, which is asked in various wall street firms e.g. Ar r ...
Barclays, Citibank, Goldman Sachs, Deutsche Bank, WellsFargo etc. Since many of you might What is differ ence betw een SQL, T-SQL and
not be familiar with Market data, Let me brief about Market data and key things which will help PL/SQL?
us to approach the problem. In simple word, Market data are the live prices of different stocks 5 w ays to Conver t Java 8 Str eam to List -
at a given moment. Since prices move very quickly in Exchange, given volume of high- Ex ample,...

frequency trading and electronic trading, market data quickly become stale. 2 Ex amples of Str eams w ith Collections in Java 8
Differ ence betw een Fir st and Second Level Cache
in...
Suppose your system is storing market data to analyze and found under-valued or over-
How to enclose a list of values into single
valued stocks to make a buying or selling decision, and you need to store only recent market quotes...
data for that purpose. Now this gives us an idea, that as soon as we receive an update, we Can You Over load or Over r ide Static methods in
should discard old market data, even if it's not yet processed. Jav...
Java Synchr onization Tutor ial : What, How and
Why?...
Since Market data is processed by another thread and it subsequently removed from the
How to Check if a Number is Binar y in Java -
store, we need to also think of a data structure which provides consume kind of functionality.
Pr ogr ...
Initially, I thought of using an unbounded Queue for storing market data, which is
What is Daemon thr ead in Java and Differ ence to
simultaneously processed by another thread, typical producer-consumer design, but this No...
design has a problem in terms of updating prices at high speed. How to get Ser vletContex t in Ser vlet, JSP, Action
...
How to Conver t an Ar r ay to Comma Separ ated
Str ing ...
Top 15 Data Str uctur es and Algor ithm Inter view
Que...
Why char acter ar r ay is better than Str ing for
Stor ...
JDBC - java.lang.ClassNotFoundEx ception:
com.mysql...
How to find Lar gest Pr ime Factor of a Number
in Ja...
Mix ing static and non static synchr onized
method -...
For example, if you receive an update before earlier market data get processed, you can not
Why use Under scor e in Number s fr om Java SE 7 -
update that, which means your application will process old data, which is stale and even if a Und...
better data is available. Though you can tackle this problem by introducing a check and What is Method Refer ences in Java 8? An Ex ample
update method i.e. if a market data for that symbol already exists then remove it and add new Differ ence betw een Str uts 1 and Str uts 2 Web
data to the queue. Devel...
10 Hiber nate Inter view Questions and Answ er s
for J...
You can do that by using the contains() method but got to be careful while overriding
How to delete a dir ector y w ith files in Java -
, because if you are wrapping symbol and price in an object say Ex a...
ith different price will be treated as differently. So Queue data structure Bitw ise and BitShift Oper ator s in Java - AND, OR,
r this problem, let me know if you come across any other solution. ...
How to Clone Collection in Java - Deep copy of
Ar r ...
LinkedHashMap you can get your order of insertion, and being
How to Remove Fir st and Last Char acter of
te Market data in constant time, but Consumer thread, which needs to
Str ing i...
market data will have a hard time over traversing, as Map will constantly
Differ ence betw een Singleton Patter n vs Static
updating. Cla...

4 of 6 8/19/2019, 6:58 PM
2 Scenario Based Data Structure and Algorithm Design Interview Questi... https://javarevisited.blogspot.com/2017/03/2-practical-data-structure-algo...

What is GET and POST method in HTTP and HTTPS


Pr ot...
Well, some of this question can be very open ended until interviewer gives some more
What is Static and Dynamic binding in Java w ith
details, but same time they also expect the candidate to ask the right questions, which mainly Ex ...
comes as what, why, when kind of queries, but remember those are very important and also 10 points on finalize method in Java – Tutor ial
appreciated by interviewers. Ex ...
What is Encapsulation in Java and OOPS w ith
Ex ampl...

That's all on this list of Practical Data structure and algorithm questions for Java, C++, and 10 ex ample of chmod command in UNIX Linux

C# developers. These are just a way of approaching the problem, you may have another Java 8 Date Time - 20 Ex amples of LocalDate,
Local...
approach or another data structure to solve these problems, and until you can justify your use
Top 50 Java Thr ead Inter view Questions Answ er s
of particular data structure in terms of benefits, you are perfectly fine to use that. In fact, for ...
improving your own solution, almost always create a better impression during interviews. How to Incr ease Console Buffer Size in Eclipse
IDE...

At the same time don't forget to prepare basic data structure and algorithms questions on How to Implement Stack in Java Using Ar r ay and
Gen...
linked list, array, queue, and stacks. Stay tuned, I will share few more such data structure
Top 10 EJB Inter view Question and Answ er asked
questions as and when I come across, mean while you are always welcome to share
in ...
something similar. Till then you can also use problems given in Algorithm Design Manual by
java.lang.UnsatisfiedLinkEr r or : no dll in
Steven S Skiena to improve your data structure and problem-solving skill. java.lib...
Reentr antLock Ex ample in Java, Differ ence
Further Learning betw een ...

Data Structures and Algorithms: Deep Dive Using Java Why w e use Thr eads in Java?

Algorithms and Data Structures - Part 1 and 2 How to r ead Pr oper ties File in Java – XML and
Tex t...
Data Structures in Java 9 by Heinz Kabutz
Top 20 FIX Pr otocol Inter view Questions
Top 10 MQ Ser ies Inter view Questions Answ er s -
Related Data Structure and Algorithm Interview Questions from Javarevisited Blog Web...
How to add or list cer tificates fr om keystor e or
Top 15 Data Structure and Algorithm Interview Questions (see here)
t...
Top 20 String coding interview questions (see here)
10 Ex amples of cur l Command in UNIX and Linux
133 core Java interview questions of last 5 years (see here)
How to cr eate Immutable Class and Object in
Top 30 Array Coding Interview Questions with Answers (see here) Java -...
Top 30 linked list coding interview questions (see here) Tibco tutor ial : Tibco RV tips and commands
Top 50 Java Programs from Coding Interviews (see here) Tibco Tutor ial: Fundamentals of Tibco RV
Top 5 books on Programming/Coding Interviews (list) messaggi...
TIBCO Tutor ial : TIBCO Rendezvous or TIBCO RV
Thanks for reading this article so far. If you like this article then please share with your friends me...

and colleagues. If you have any question or doubt then please let us know and I'll try to find an What is the use of class “java.lang.Class” in
Java...
answer for you.
Why Compar ing Integer using == in Java 5 is Bad?
What is Abstr action in Java? Abstr act Class or
Int...
Fundamentals of Object Or iented Pr ogr amming
in Jav...
How to check if a thr ead holds lock on a
par ticula...
Why Str ing is Immutable or Final in Java
Differ ence betw een map() and flatMap() in Java 8
S...
Covar iant Method Over r iding of Java 5 - Coding
Bes...
10 Singleton Patter n Inter view Questions in Java
-...
What is differ ence betw een Enumer ation and
Iter ato...
What is differ ence betw een HashMap and
Hashtable i...
java.lang.NoSuchMethodEr r or : main Ex ception in
thr ...
How to cr eate and ex ecute JAR file in Java –
Comma...
inter view questions , pr ogr amming 130+ Java Inter view Questions Answ er s for 2 to
7 Y...
How to for mat Decimal Number in Java -
DecimalFor m...
Steps to Cr eate JUnit Test in Eclipse and
Netbeans...

5 of 6 8/19/2019, 6:58 PM
2 Scenario Based Data Structure and Algorithm Design Interview Questi... https://javarevisited.blogspot.com/2017/03/2-practical-data-structure-algo...

Java Mistake 1 - Using float and double for


Enter your comment... moneta...

► Febr uar y (87)


► Januar y (104)

Comment as: ► 2016 (115)


pruthv i.patil@techpalle.com (Google) Sign out

Publish Preview Notify me


Refer ences

1. Or acle's Java Tech Netw or k


2. jQuer y Documentation
3. Micr osoft SQL Ser ver Documentation
4. Java SE 8 API Documentation
New er Post Home Older Post 5. Spr ing Documentation
6. Or acle's JAva Cer tification
7. Spr ing Secur ity 5 Documentation
Subscr ibe to: Post Comments ( Atom )
Pag es

Pr ivacy Policy

Copyr ight by Javin Paul 2010-2018. Pow er ed by


Blogger .

Search This Blog


Search

6 of 6 8/19/2019, 6:58 PM

You might also like