Oracle Interview Question for Java Developers
- 0of 0 votes
AnswersImplement entity tag cache in following two assignments. Design cache for
- gzagarwal January 08, 2014 in United States
read performance. Also, ensure that it takes as little memory as possible.
Write a caching class EntityTagCache that is supposed to cache tags for a given
id. Also provide TestEntityTagCache JUnit class that we can use to verify the
functionality. We only need above two files ( e.g. no need for build file ), as
we'll go through your solution in an Eclipse Java project.
An entity and its tags have following characteristics:
- A given entity is identified by an int id. The id is >= 0.
- An entity can have 0-N tags, where each tag is a string value. E.g. for
id '1', we can have tags like 'foo' & 'bar'. There are around 1000 unique
tags in 100 million entities, but the actual number can vary by a few thousand.
- The EntityTagCache does not need to consider id/tag eviction. It is expected
for the cache to throw a OutOfMemoryError if more tags exist than we can cache
in memory. However, the design should ensure that it takes as little memory
as possible to cache the tags.
- There can be maximum of 100 million entities in the cache and the id value
cannot be more than the number of entities in the cache and is >= 0. Thus,
if we've to cache 100 entities, then id is from 0 to 99.
- We should be able to load our initial data into the cache when we create
EntityTagCache. We should pass a file to the cache that contains entity
information. E.g.
entities.csv
------------
1,foo,bar
2,abc,foo
3
4,xyz,bar
In above, in first line entity id is 1 and tags are foo, and bar. For entity id 3
there are no tags.There are four unique tags 'foo', 'bar', 'abc', and 'xyz'.
EntityTagCache cannot be updated once it has all the data. Also, it
does not need to have any notion of eviction, as we require that all the
data must be loaded in memory. The cache only provides the following
public method:
1. 'getTags' method that takes an id and returns the tags for the entity.
This method on worst case ( barring garbage collection) take a few
100 nano seconds. It can be called 1000's of time in a few milliseconds.
The cache should be designed for multi threaded access with 1000's
of requests to getTags in a few ms.| Report Duplicate | Flag | PURGE
Oracle Java Developer
Interview Type: Phone Interview