According to wikipedia, a cache is a hardware or software component that stores data so that future requests for that data can be served faster.

Benefits:

  • Faster access of data in O(1)
  • Computation complexity once for the first time

Types:

  • Memory cache
  • Database cache
  • Disk cache, etc

To create a cache, we can simply use a map / dictionary data structure and we can get the expected result of O(1) for both get and put operation.

But, we can’t store everything in our cache. …


There are multiple ways to find the pair of numbers in a given array. The numbers in the array can be in two ways.

  1. Sorted
  2. Unsorted

Brute force:

Brute force approach will check each and every number to determine if it equals to the given sum. The complexity of this approach is O(n²).

public void findPair(int[] array, int sum) { for(int i = 0; i < array.length - 1; i++)…

HashMap is a dictionary data structure provided by java. It’s a Map-based collection class that is used to store data in Key & Value pairs. In this article, we’ll be creating our own hashmap implementation.

The benefit of using this data structure is faster data retrieval. It has data access complexity of O(1) in the best case.

In layman’s terms, a for each key we get the associated value.

To implement this structure,

  1. We need a list to store all the keys
  2. Key — Value relationship to get value based on key


Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

Solution:

Let’s start with a brute-force solution.

public int[] productExceptSelf(int[] nums) {
int[] arr = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
int mul = 1;
for (int j = 0; j < nums.length; j++) {
if(i == j) {
continue;
}
mul *= nums[j];
}
arr[i] = mul;
}
return arr;
}

Mahfuzur Rahman

Software developer, passionate learner. www.mahfuz.info

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store