Category: fullstackdevelopment
-
Data Structures & Algorithms in Java – Graphs – Valid Tree
Problem: Given the number of nodes in a graph and the list of edges in the graph , find if it is a valid tree. For example, Given n = 5 and the edges [[0,1] ,[0,2],[0,3],[3,4] It is a valid tree because there are no cycles in it and every node is connected with an…
-
Data Structure & Algorithms in Java – Graphs – Number of Connected Components in an Undirected Graph
Problem: Given a list of edges of a undirected graph and the number of nodes in it , find out the number of connected components in the graph. For example: Given the number of nodes 5 And the edges: [[0,1],[1,2],[3,4]] The number of components is 2. You can understand this if you visualize the final…
-
Data Structures & Algorithms in Java – Longest Consecutive Sequence
Problem Given an array of numbers, find the length of the longest consecutive sequence For example , Given the input [100,4,200,1,3,2] The longest consecutive sequence is [1,2,3,4] So the length is 4. Notice that the sequence need not be in order and can be scattered across the array. Try out the solution here: Leetcode Solution…
-
Data Structures & Algorithms in Java – Graphs – Number of Islands
Problem: Given a m * n grid representing a map with the value 1 representing land and the value 0 representing water, find out the number of islands in the grid. That is find out the piece of area containing ‘1’s surrounded on all sides by ‘0’s or the grid boundaries (Assume the entire grid…
-
Data Structures & Algorithms in Java – Graphs – Pacific Atlantic Water Flow
Problem: There is a rectangular island of size m * n where m is the length and n is the breadth. The island is surrounded by Pacific Ocean on the left and the top and the Atlantic Ocean on the right and bottom. The m * n island is divided into m * n grids…
-
Data Structures & Algorithms in Java – Graphs – Course Schedule
Problem: Given the number of courses to take in a class and the dependency of the courses (before taking one course you should have taken another course) find out if all the courses can be taken. The dependency is mentioned by a list of pairs in the form [ [a1,b1],[a2,b2] …] where to take the…
-
Data Structures & Algorithms in Java – Graphs – Clone Graph
Problem: Given a graph , clone it. For example , consider a graph represented by the below adjacency list: [ [2,4],[1,3],[2,4],[1,3] ] You need to create a clone with the same adjacency list as above. An adjacency list is a list containing a list of neighbors of each node in a graph. So in the…
-
Data Structures & Algorithms in Java – Heap – Top K Frequent Elements
Problem: Given an array of numbers , find the k most frequent numbers . For example , Given the array: [1,1,1,2,2,3,4,5] and the value k = 2 The output is [1,2] Because these are the most frequent numbers. 1 occurs thrice and 2 occurs twice. Rest other numbers occur only once. So the most frequent…
-
Data Structures & Algorithms in Java – Matrix – Word Search
Problem Given a n*n matrix containing characters, find if the given word can be found in any direction For example , The output for above problem is true since the word ABCGK can be found in the matrix. Try out the solution here: https://leetcode.com/problems/word-search/ Solution: The solution to this problem is backtracking You start with…
-
Data Structures & Algorithms in Java – Matrix – Rotate Image
Problem: Given a n * n matrix representing an image , rotate the matrix by 90 degrees . Constraints: Do not use any extra memory in proportion to the input size. That is , space complexity should be 1. For example, For the input : [ [1,2,3], [4,5,6], [7,8,9]] The output is : [[7,4,1], [8,5,2],…