Category: Java programming question
-
Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
Problem Statement You are given the root of a binary tree and an integer array arr.You need to determine whether the array arr represents a valid path from the root of the tree to any leaf node. A valid path means: If such a path exists, return true; otherwise, return false. Example Input: Output: Explanation:There…
-
Binary Tree Maximum Path Sum
Problem Statement You are given the root of a binary tree.A path in a tree is a sequence of nodes where each pair of adjacent nodes in the sequence has a parent-child relationship. A path does not necessarily need to pass through the root, and it can start and end at any node.The path sum…
-
First Unique Number
Problem StatementYou are given a queue of numbers, and you need to find the first unique number in the queue at any given time.You must also support two operations: The goal is to design an efficient data structure to handle both operations quickly. ExampleInput: Output: Understanding the Problem We need to track numbers in the…
-
Maximal Square
Problem StatementYou are given a 2D binary matrix filled with ‘0’s and ‘1’s.Your task is to find the largest square containing only ‘1’s and return its area. ExampleInput: Output:4Explanation: The largest square containing only ‘1’s has a side length of 2, so its area = 2 × 2 = 4. Understanding the Problem We have…
-
Longest Common Subsequence
Problem StatementYou are given two strings text1 and text2. Your task is to return the length of their longest common subsequence.A subsequence of a string is a new string that is formed by deleting some (or none) of the characters without changing the order of the remaining characters. ExampleInput:text1 = “abcde”, text2 = “ace”Output:3Explanation: The…
-
Jump Game
Problem StatementYou are given an integer array nums, where each element represents the maximum number of steps you can jump forward from that position.Your task is to determine if you can reach the last index starting from the first index. ExampleInput: nums = [2, 3, 1, 1, 4]Output: trueExplanation: Start at index 0, jump 1…
-
LRU Cache
Problem Statement Design a Least Recently Used (LRU) Cache data structure. You need to implement a class LRUCache with the following operations: Example Output: [1, -1, -1, 3, 4] Understanding the Problem An LRU Cache should do two things efficiently: If the cache is full, we must remove the least recently used key before inserting…
-
Bitwise AND of Numbers Range
Problem Statement You are given two integers left and right representing a range [left, right] (inclusive).You need to find the bitwise AND of all numbers in this range. Example Explanation: Understanding the Problem The bitwise AND (&) operation compares each bit of two numbers: If we take the AND of a continuous range of numbers,…
-
Subarray Sum Equals K
Problem Statement You are given an integer array nums and an integer k.Your task is to find the total number of continuous subarrays whose sum equals k. Example There are two subarrays that sum up to 2: [1,1] (from index 0 to 1) and [1,1] (from index 1 to 2). 1. Understanding the Problem A…
-
Create a Map of Department to Highest-Paid Employee Using Streams
When working with a list of employees, a common requirement is to find the highest-paid employee in each department. Java Streams provide concise ways to do this using groupingBy and maxBy. Assume the following Employee class: And a list of employees: Approach 1: Using groupingBy + maxBy (Optional Result) Explanation: Approach 2: Using collectingAndThen to…
