Tip 1: Practice at least 250 questions.
Tip 2: Practice some AI fundamentals.
Tip 3: Do at least 2 projects.
Tip 1: Have some projects on your resume.
Tip 2: Do not include false information on your resume.



1) It is also possible that the tree doesn’t grow any apples on any particular day, i.e., ’APPLES[i]’ = 0 for ‘i’th day.
2) ‘DAYS[i]’ = 0 if and only if ‘APPLES[i]’ = 0.
3) You can keep eating apples even after ‘N’ days, but keep in mind that you can eat at most one apple per day.
This is an Expected Value on a Tree problem. Because the process stops whenever you reach a leaf node or a node where all neighbors have already been visited, each unique path from the root (node 1) has a specific probability of occurring.
Problem Statement
Title: Expected Number of Apples Collected on a Random Walk
You are given a tree rooted at node 1. Each node i contains arr[i] apples. You start at node 1 and move to an unvisited neighbor chosen uniformly at random. The walk ends when you reach a node with no unvisited neighbors. Calculate the expected total number of apples eaten.
Solution in Steps
Step 1: Understand the Path Probability
In a tree, because you can only visit each node once, you are always moving "down" from the root toward the leaves. There is only one unique path from the root to any node u.
Let P(u) be the probability that node u is included in your walk.
For the root (node 1): P(1)=1.
For any other node u: You can only reach u if you were already at its parent v.
The probability of moving from v to u is degree(v)−11 (excluding the edge back to v's own parent). For the root, it is degree(1)1.
Step 2: Define the Recursive Probability
Let children(v) be the set of children of node v, and Cv be the number of children node v has.
The probability P(u) of visiting node u is:
P(u)=P(parent(u))×Cparent(u)1
Essentially, the probability of reaching any node is the product of the inverse of the number of choices made at every ancestor node along the path.
Step 3: Apply Linearity of Expectation
The expected total number of apples is the sum of the expected apples collected from each node individually.
Expected Total=i=1∑nE[apples from node i]
Since the apples at node i are either all eaten (if visited) or none are eaten (if not visited):
E[apples from node i]=arr[i]×P(i)
Therefore:
Expected Total=i=1∑n(arr[i]×P(i))
Step 4: Implementation (DFS)
Perform a DFS starting at node 1.
Pass the current path probability down to the children.
For each node u, calculate P(u)×arr[u] and add it to a global sum.
If a node v has k children, each child receives P(v)×k1 as its visit probability.



1. Choose an element from the start or end of the array and the value of the element to your score.
2. Remove the element from the array you have chosen in step – 1.
Initially, you have a score of zero.
Step 1: Invert the Problem (Minimize Skips)
It is often easier to think about this as: Total Score = (Sum of all hurdles) - (Minimum sum of hurdles skipped)
To ensure you never take more than k consecutive hurdles, you must pick at least one hurdle to skip in every window of size k+1.
Step 2: Define DP State
Let dp[i] be the minimum total value of hurdles skipped such that the constraint is satisfied up to index i, and hurdle i is definitely skipped.
For the current hurdle i to be the skip that breaks a sequence, the previous skip must have occurred at some index j such that the number of hurdles between j and i is ≤k.
This means j must be in the range [i−(k+1),i−1].
Step 3: Transitions and Optimization
The transition is:
dp[i]=arr[i]+i−(k+1)≤j
To solve this in O(n) instead of O(n⋅k):
Use a Sliding Window Minimum (Monotonic Queue/Deque).
The Deque will store indices of dp values in increasing order of their values.
For each i, the minimum value in the required range is at the front of the Deque.
Step 4: Final Calculation
After filling the dp array up to n, the minimum total skip value will be the minimum of dp[j] where j is in the last k+1 indices (since one of the last k+1 hurdles must be skipped).Final Max Score = ∑i=1narr[i]−min(last k+1 skip options).


1- Select a element arr[i] and add it to your score.
2- Delete every element equal to arr[i] - 1 and arr[i] + 1.
You are given arr = {5, 6, 7, 8}. Then our answer will be 14.
First select arr[1] = 6 and add it to your score. Score will be 6. Now, we also have to delete all elements equal to arr[1] - 1 = 5 and arr[i] + 1 = 7.
In next move select arr[3] = 8. Delete it and add it to your score. Score = 14. Now, no element is left to be deleted.
So, our final score is 14, which is the correct answer.
Step 1: Analyze the Contribution of Each Element
Instead of looking at the splits, look at how many times each individual element arr[i] is added to the total score.
At the very first level (the whole array), every element is added once.
Once you split the array, an element will be part of a new subarray. Every time a subarray is split again, its elements are added to the score once more.
An element arr[i] stops contributing to the score only when it ends up in a subarray of length 1.
Step 2: Recognize the Tree Structure
The splitting process can be viewed as a Binary Tree where:
The root is the entire array.
Each leaf is an individual element arr[i].
The depth of a leaf (distance from the root) represents how many times that specific element was included in a "sum" before it became a standalone array.
Total Score = ∑i=1n(arr[i]×depth of arr[i]).
Step 3: The Maximum Score Strategy
To maximize ∑(arr[i]×depthi), we want the elements with the largest values to have the greatest depths.
In a binary tree with n leaves, the maximum possible depth for an element is n−1.
This happens in a "skewed" tree where at every step, you peel off exactly one element and keep the rest of the array together.
To maximize the score, you should always peel off the smallest available element, keeping the larger elements together so they can be added again in the next round.
Step 4: The Final Calculation
If you always split the array into {smallest_element} and {rest_of_the_array}, the largest element will be added n−1 times, the second largest n−2 times, and so on (down to the two smallest elements, which will both have the same maximum depth in the final split).
However, the most straightforward way to calculate this without sorting is to realize that in any split strategy, the total score is simply the sum of the array at every single internal node of the partition tree. For the maximum score:
Sort the array in ascending order.
The score is ∑i=2n(prefix_sum up to i)+arr[1].
(Alternatively, simply realizing that you want a skewed tree makes the calculation O(nlogn) due to sorting.

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
What does the SQL function NOW() return?