# Memoization example in java

If you want to practice data structure and algorithm programs, you can go through Java coding interview questions.

In this tutorial, we will see about Memoization example in java.

Would you like to do same task again and again when you know that it is going to give you same result? I think Answer will be No.

So `Memoization` ensures that method does not execute more than once for same inputs by storing the results in the data structure(Usually `Hashtable` or HashMap or `Array`).
Let’s understand with the help of Fibonacci example.
Here is sample fibonacci series.

0,1,1,2,3,5,8,13,21,34,55,89,144..

So it has recurrence relation of:

F(n)= F(n-1)+F(n-2)

So Let’s write recurrence function for it.

When you run above code with n=5, you will get below output.

Calculating fibonacci number for: 5
Calculating fibonacci number for: 4
Calculating fibonacci number for: 3
Calculating fibonacci number for: 2
Calculating fibonacci number for: 2
Calculating fibonacci number for: 3
Calculating fibonacci number for: 2
Fibonacci value for n=5: 5

As you can see, we are calculating fibonacci number for 2 and 3 more than once.

Let’s draw a `recursive tree` for fibonacci series with n=5.
Here two children of node will represent recursive call it makes. If you notice here, we are calculating `f(3)` twice and `f(2)` thrice here, we can avoid duplication with the helping of caching the results.
We will use one instance variable memoizeTable for caching the result.

• Check if `n` is already present in `memoizeTable`, if yes, return the value
• If `n` is not present in `memoizeTable`, then compute and put the result in `memoizeTable`.

When you run above program, you will get below output.

Putting result in cache for 2
Putting result in cache for 3
Getting value from computed result for 2
Putting result in cache for 4
Getting value from computed result for 3
Putting result in cache for 5
Fibonacci value for n=5: 5

As you can see, we are not computing fibonacci number for 2 and 3 more than once.
That’s all about `Memoization` in java.

## Related Posts

• 18 June

### Maximum Number of Vowels in a Substring of Given Length

Table of ContentsApproach – 1 Generate All Substrings Using substring() MethodApproach – 2 Using Sliding Window Method (Linear Time Solution) In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum Number of Vowel […]

• 04 June

### Search for a range Leetcode – Find first and last position of element in sorted array

Table of ContentsApproach 1 (Using Linear Search)Approach 2 (Using Modified Binary Search-Optimal) In this article, we will look into an interesting problem asked in Coding Interviews related to Searching Algorithms. The problem is: Given a Sorted Array, we need to find the first and last position of an element in Sorted array. This problem is […]

• 30 April

### Convert Postfix to Infix in Java

Learn about how to convert Postfix to Infix in java.

• 30 April

### Convert Prefix to Postfix in Java

Learn about how to convert Prefix to Postfix in java.

• 16 April

### Data Structures in java

Table of ContentsArray Declare and initialize array in javaAdvantages of arrayDisadvantages of array ExampleArray practice programsStackStack implementation using ArrayStack implementation using LinkedListImplementationPractice ProgramsQueueQueue implementation using arrayQueue implementation using LinkedListImplementationLinkedListImplementationLinkedList Practice ProgramsBinary treeImplementationBinary tree practice programsBinary Search treeImplementationBinary search tree Practice programsTrieImplementationHeapImplementationGraphImplementation Inbuild data structures in javaStringHashMapLinkedHashMapArrayListLinkedListHashSet In this post, we will see about various data […]

• 29 November

### Top 100+ Java coding interview questions

Table of ContentsStringQuestion 1 : How to reverse a String in java? Can you write a program without using any java inbuilt methods?Question 2 : Write a java program to check if two Strings are anagram in java?Question 3 : Write a program to check if String has all unique characters in java?Question 4 : […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.