jsartisan / frontend-challenges

FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
https://frontend-challenges.com
43 stars 5 forks source link

Jump Game #309

Closed jsartisan closed 3 hours ago

jsartisan commented 4 hours ago

Info

difficulty: medium
title: Jump Game
type: question
template: typescript
tags: javascript, blind75, dynamic-programming, array, greedy

Question

Given an array nums where each element represents the maximum jump length at that position, determine if you can reach the last index starting from index 0.

Rules:

Constraints:

Examples:

// Example 1:
console.log(canJump([1,2,0,1,0]));
// Output: true
// Explanation: Jump path: 0->1->3->4

// Example 2:
console.log(canJump([1,2,1,0,1]));
// Output: false
// Explanation: Can't reach last index

Template

index.ts

export function canJump(nums: number[]): boolean {

}

index.test.ts

import { canJump } from './index';

describe('canJump', () => {
  test('Example 1: Can reach end', () => {
    expect(canJump([1,2,0,1,0])).toBe(true);
  });

  test('Example 2: Cannot reach end', () => {
    expect(canJump([1,2,1,0,1])).toBe(false);
  });

  test('Single element', () => {
    expect(canJump([0])).toBe(true);
  });

  test('All zeros except first', () => {
    expect(canJump([2,0,0])).toBe(true);
  });

  test('Need to use full jumps', () => {
    expect(canJump([3,2,1])).toBe(true);
  });

  test('Trapped by zero', () => {
    expect(canJump([1,0,1,0])).toBe(false);
  });

  test('Multiple possible paths', () => {
    expect(canJump([2,3,1,1,4])).toBe(true);
  });

  test('Exactly enough jumps', () => {
    expect(canJump([1,1,1,1])).toBe(true);
  });

  test('Zero at start', () => {
    expect(canJump([0,1,1])).toBe(false);
  });

  test('Long sequence', () => {
    expect(canJump([2,0,2,0,4,0,0,0,4])).toBe(false);
  });
});
github-actions[bot] commented 4 hours ago

310 - Pull Request updated.