Home
Gaurav's GitHub Page
Cancel

Bubble Sort

Problem Description Implement Bubble Sort (in-place) Solution We can loop through the array and swap the elements if the right one is lesser than the one towards it’s left. We can further optimi...

Power Set

PROBLEM DESCRIPTION For a given list of numbers, return the list of its power set. SOLUTION APPROACH 1 This can be solved recursively. We call the function twice, once assuming that the current...

Invert Binary Tree

PROBLEM DESCRIPTION Invert the binary tree by swapping every every left node in the tree with its right node. SOLUTION import java.util.*; class Program { public static void invertBinaryTree(...

Min Height BST

PROBLEM DESCRIPTION Given a list of integers in sorted order, return the root node of a BST such that the BST has the minimum possible height. SOLUTION Since the list of already sorted, we can i...

Validate BST

PROBLEM DESCRIPTION Given a tree, check whether it’s a BST. SOLUTION APPROACH 1 MaxOf(Left Sub Tree) < Node.value <= MinOf(Right Sub Tree) Using this relation, for each Node, we can ...

Find kth Largest Value in BST

PROBLEM DESCRIPTION Given a BST, find the kth largest element in that BST. SOLUTION The trivial way is to do a inorder traversal of the BST - which will be sorted. Then return the (n-k)th elemen...

Palindrome LinkedList

PROBLEM DESCRIPTION Given the head of a singly linked list, return true if it is a palindrome. Palindrome LinkedList SOLUTION Using Stack (O(n) space complexity) class Solution { public ...

Reverse a LinkedList

PROBLEM DESCRIPTION Reverse a given LinkedList. Reverse a LinkedList SOLUTION ITERATIVE APPROACH class Solution { public ListNode reverseList(ListNode head) { if(head ==...

Squares of a Sorted Array

Problem Description Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Squares of a Sorted Array Solution T...

Remove nth Node From End of LinkedList

PROBLEM DESCRIPTION: Remove nth Node From End of LinkedList Given a LinkedList and an integer n, remove nth Node from the end of the list. SOLUTION One approach that will work is to first get th...