I came across these two questions and found similarities.

  1. Buy and Sell only 1 time to get maximum profit.
  2. Buy and Sell multiple times to get maximum profit.

Here comes the solutions for the same, The code is the same for both the questions but with minor differences.

Solution 1: Buy and Sell single time:

This is a good question, So decided to solve it.

Given two sorted arrays, such that the arrays may have some common elements. Find the sum of the maximum sum path to reach from the beginning of any array to end of any of the two arrays. We can switch from one array to another array only at common elements.
Note: The common elements do not have to be at the same indexes.
Expected Time Complexity: O(m+n), where m is the number of elements in ar1[] and n is the number of elements in ar2[].


This is marked as Hard on the geek's platform.

Given an odd integer n, find a matrix of size n x n with the following conditions:

  1. Each cell contains an integer from 1 and n (inclusive).
  2. No integer appears twice in the same row or the same column.
  3. All 1’s must be at every possible distance from the center of the matrix. The center of a n x n square is cell ((n-1)/2, (n-1)/2) for odd n.

Example :

This question is based on ladder and stairs, We need to find the minimum number of jumps to reach the end.

Here we will follow few steps:

  1. initialize stairs and ladder as the first element of the array.
  2. Keep track of the maximum ladder we got as of now.
  3. decrease the stairs as we climb up.
  4. if stairs become zero in the current ladder make a jump to another ladder and recalculate the stairs left in that ladder to reach the top(stairs = ladder level)

Below is the implementation of the code.

This question is similar to check if two trees are symmetric or not.


Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree that consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.

Example 1:
Given tree s:

Given tree t:

Return true, because t has the same…

I came across these 2 questions and found a similarity in the logic part.

  1. Symmetric Tree:

We need to check symmetry for the same tree or say mirror image.

Here comes the solution for the same.

I came across these 2 questions and found similarities in both of them.

Now, let's discuss the solution:

  1. Check for a balanced tree:

Here First we need to find the height of the binary tree for both part i.e. left tree and right tree for each node, if the difference between them is greater than 1 then it is not balanced tree, return false.

How to find the height of the binary…

Recently I study the topic and sharing the notes for the same.

Inner Classes

Without existing one type of object if there is no chance of existing another type of object then we should go for inner classes.
Note(we can declare anything inside anything means class inside interface or interface inside the class)
Ex: 1 class Car{
class Engine{}
Here without Car object, there is no chance of an Engine object.

Ex: 2 Map Interface: A map is a group of key-value pair and each key-value pair is called an entry, without an existing map object there is no…

I came across this question in an interview recently for the SDE-2 position.

There is a shop with old-style cash registers. the price of each item is typed manually. Given a list of items and their correct prices, compare the prices to those entered when each item was sold. Determine the number of errors in the selling price.

Here Comes the Solution for the same.

Use 2 HashMap, one for Storing the original product name and product price, HashMap<String, Float> map2 = new…

Here are the notes I made after studying the Generics concept in java.

1. Introduction.
2. Generic Classes.
3. Bounded Types.
4. Generic Methods and Wild Card Characters.
5. Communication with non-generic code.
6. Conclusions.

Generics: The main Objective of Generics is
1. To provide type-safety and
2. To resolve the type-casting problem.
1. Introduction:

Type-casting is a headache in the collection, So to overcome this SUN people introduces the Generics concept in the 1.5 version.

Arrays are type-safe we can give the guarantee of type of elements present inside the array(except objects type)
Ex: If we…

Vipul Gupta

Software Developer

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