Monday, July 30, 2018
Thursday, July 26, 2018
Least Recently Used (LRU) cache
Below java code is a simple implementation of Least Recently Used (LRU) cache using HashMap and LinkedList.
For simplicity i am taking [Integer, String] as a key value pair for the cache, you can use any other structure as per your requirement.
LRU cache requirement -
get(key) will return the value if present otherwise will return 'NA'
set(key,value) will insert the value if it is not already present else update the value. Also when cache reaches its MAX capacity it should remove least recently used value before inserting a new value.
Logic -
HashMap will contain the key,value pair of the cache and LinkedList will be used to track the usage. As you know LinkedList is a Doubly-linked list implementation of the List which maked insertion and deletion faster. Here first item will be the oldest item whereas last item will be the most recent item.
Data.java
For simplicity i am taking [Integer, String] as a key value pair for the cache, you can use any other structure as per your requirement.
LRU cache requirement -
get(key) will return the value if present otherwise will return 'NA'
set(key,value) will insert the value if it is not already present else update the value. Also when cache reaches its MAX capacity it should remove least recently used value before inserting a new value.
Logic -
HashMap will contain the key,value pair of the cache and LinkedList will be used to track the usage. As you know LinkedList is a Doubly-linked list implementation of the List which maked insertion and deletion faster. Here first item will be the oldest item whereas last item will be the most recent item.
Data.java
Labels:
Interview questions
Wednesday, July 25, 2018
Simple junit test case with mockito
Mocking is a way to test the functionality of a class in isolation. Mockito facilitates creating mock objects seamlessly. It uses Java Reflection in order to create mock objects for a given interface. Mock objects are nothing but proxy for actual implementations. To know more about mockito, click here.
Maven dependiencies
This is simple junit testing, we will see integration testing in a different article.
Greeting.java
This is the Greeting.java class for which we are writing junit test cases
GreetingTest.java
Maven dependiencies
This is simple junit testing, we will see integration testing in a different article.
Greeting.java
This is the Greeting.java class for which we are writing junit test cases
GreetingTest.java
Find a pair of elements from an array whose sum equals to a given number
The best way would be to insert every element into a hashset. Then for every x, we can just look up its complement, T-x.
Overall the Time Complexity of this approach is O(n).
Overall the Time Complexity of this approach is O(n).
Labels:
Interview questions
Simple block chain code with Java
A blockchain is the structure of data that represents a financial ledger entry, or a record of a transaction. Block chain contains list of blocks, each block will contain transaction and a hashcode of previous block. similarly next block will contain its transaction and hashcode of current block thus if any of the transaction value gets change in blockchain subsequent blocks hashcode will change.
If you dont know about hashcode, The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure.
Transaction.java - It contains transaction information
If you dont know about hashcode, The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure.
Transaction.java - It contains transaction information
Labels:
Block chain
Friday, July 13, 2018
Spring boot web application with thymeleaf
In this article we will see a sample spring boot web application with front end as thymeleaf. To know more about thymeleaf, click here.
Note you can use any other view technology like JSP instead of thymeleaf as per your requirement
Project structure -
Labels:
SpringBoot
Subscribe to:
Posts (Atom)