Suppose we are playing a two-player game where there are n number of marbles and in each round, a player has to take a positive square number of marbles. If a player can't take that square number of marbles, he/she loses. So, given a number n, we have to find out if we can win the game or not. We always make the first turn and select an optimal number of marbles. So, if the input is like 14, then the output will be True. Because at the first turn, we take 9 marbles. That leaves 5 marbles from ... Read More
In road trip planning, we often need to find the shortest path between cities while minimizing intercountry travels. This problem combines graph traversal with country border crossing penalties to find both the minimum border crossings and total cost. Problem Understanding We have a list of roads R where each element is (x, y, cost) representing a road from city x to city y with given cost. We also have a list C where each element contains cities belonging to the same country. Given a starting city s and destination city e, we need to find the minimum intercountry ... Read More
Currency conversion is a common problem in financial applications. Given three arrays containing source currencies, destination currencies, and their conversion rates, we need to find the conversion rate between any two currencies using the available exchange paths. The problem provides curr_a (source currencies), curr_b (destination currencies), and conv_rate (conversion rates). We need to find the conversion rate from a source currency to a destination currency, returning 0 if no path exists. Problem Analysis If the input is src = "INR", dest = "JPY", curr_a = ["INR", "GBP", "EUR"], curr_b = ["GBP", "EUR", "JPY"], conv_rate = [0.009, 1.17, ... Read More
Suppose we have two lists nums1 and nums2. Each of these two lists represents a vector in run-length encoded form. For example, a vector [1, 1, 1, 2, 2, 2, 2] is represented as [3, 1, 4, 2] (because there are 3 ones and 4 twos). We need to find the dot product of these two vectors, which is the sum of element-wise multiplication of items present in two vectors. So, if the input is like nums1 = [2, 7, 5, 3] and nums2 = [3, 5, 4, 2], then the output will be 109 because: The vectors ... Read More
Suppose we have an edge list of a directed graph with n nodes numbered from 0 to n-1. Given two integer values a and b, we need to check whether there exists a node c such that we can reach both a and b from c. 0 1 2 3 4 ... Read More
When we have a 2D matrix where each cell represents coins, we need to find the maximum coins we can collect with specific constraints. When we pick coins from matrix[r, c], all coins in adjacent rows (r-1 and r+1) and adjacent columns (c-1 and c+1) disappear. This problem can be solved using dynamic programming by treating it as two separate "House Robber" problems − first for each row, then for the resulting row sums. Problem Understanding Given the input matrix ? 2876 101042 5923 We can pick cells with coins 8, 6, 9, ... Read More
Suppose we have two lists: cores and tasks. The cores[i] indicates the number of cores available in the ith server, and tasks[i] indicates the number of cores needed to execute that task. Each task must be run on only one server, and a server may have multiple tasks to run. We need to check whether it's possible to run all tasks with the given server cores. For example, if cores = [10, 7] and tasks = [7, 3, 2, 2, 1], the output will be True because we can assign tasks[0] (7 cores) and tasks[1] (3 cores) to the ... Read More
Suppose we have a circular list called nums where the first and last elements are neighbors. Starting from any index i, we can move nums[i] number of steps forward if nums[i] is positive, or backwards if it's negative. We need to check whether there is a cycle with length greater than one that moves only in one direction (either all forward or all backward). For example, if the input is nums = [-1, 2, -1, 1, 2], the output will be True because there is a forward path: [1 → 3 → 4 → 1]. Algorithm Steps ... Read More
Sometimes we need to count characters at different bracket depths in a string. This problem involves parsing a string with balanced brackets "(", ")" and characters "X", then counting how many "X" characters appear at each depth level. So, if the input is like s = "(XXX(X(XX))XX)", then the output will be [5, 1, 2] Bracket Depth Visualization ( X X X ( X ( X X ) ... Read More
Suppose we have a lowercase string s and another list of integers called shifts whose length is same as the length of s. Here each element in shifts[i] indicates it to shift the first i + 1 letters of s by shifts[i] positions. If shifting crosses 'z' it will wrap up to 'a'. We have to find the resulting string after applying shifts to s. So, if the input is like s = "tomato" shifts = [2, 5, 2, 3, 7, 4], then the output will be "qjcoes". After shifting first character 2 places, it will be 't' to ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance