Data Structures & AlgorithmsEasyAccenture · Cognizant coding round pattern
Problem: Can you reach the last index if a[i] is the maximum jump from i?
Problem: Can you reach the last index if a[i] is the maximum jump from i?
static boolean canReach(int[] a) {
int far = 0;
for (int i = 0; i < a.length; i++) {
if (i > far) return false;
far = Math.max(far, i + a[i]);
}
return true;
}
// canReach({3, 2, 1, 0, 4}) + " " + canReach({2, 0, 0}) + " " + canReach({0, 1})What does the commented expression evaluate to?- A. false true true
- B. false true false
- C. true true false
- D. false false false
Correct answer: B. false true false
Track the farthest index reachable so far; if i passes it, i is unreachable. In {3,2,1,0,4} every path lands on index 3 (value 0) and far stays 3. {2,0,0} reaches index 2 directly. {0,1} is stuck at 0. O(n).
More Coding Interview Problems questions
- Problem: Coins of denominations {1, 7, 10} are available in unlimited supply. Pay 14 with the fewest coins. What does the greedy 'largest…
- Problem: Split a string into as many parts as possible so each letter appears in at most one part; return the part sizes. python def…
- Problem: Jobs have deadlines and profits; each takes one unit of time. Schedule to maximise profit (greedy by profit, latest free slot)…
- Problem: Given n ≤ 10⁵ intervals, find the minimum number of points such that every interval contains at least one point. Which greedy…
- Problem: Given n balloons as horizontal intervals, find the minimum number of vertical arrows that burst all of them (a balloon bursts if…
- Problem: Insert a new interval into a sorted list of non-overlapping intervals, merging if necessary. python def insert(iv, new): out, i…