cswork / libraryOpen practice workspace

Coding questions · Meta

Find K-th Largest and Longest Vacation

This pair of problems evaluates array manipulation and algorithmic problem-solving skills, specifically order-statistics for selecting the k-th largest element and maximizing consecutive sequences under constrained transformations for the vacation streak problem.

Solve the following two coding problems.

  1. Find the k-th largest element

    Given an integer array nums and an integer k, return the element that would appear at index k if the array were sorted in descending order.

    Assume k is 0-indexed, so:

    • k = 0 means the largest element
    • k = 1 means the second largest element
    • and so on

    Example:

    • nums = [5, -3, 9, 1]
    • k = 0 -> 9
    • k = 1 -> 5
    • k = 3 -> -3
  2. Maximize the longest vacation using PTO

    You are given a calendar year represented as a character array containing only:

    • H = holiday
    • W = workday

    You are also given an integer pto, representing how many workdays you may convert into vacation days by using Personal Time Off.

    Your goal is to maximize the length of the longest consecutive vacation streak, where a vacation day is either:

    • an existing holiday (H), or
    • a workday (W) that you choose to cover with PTO

    Return the maximum possible length of such a consecutive streak.

    Example:

    • calendar = [W, H, H, W, W, H, W]
    • pto = 2
    • Output: 5

    Explanation: By using PTO on two appropriate workdays, you can create a longest contiguous vacation block of length 5.

Zero-Indexed Kth Largest

Return the element at index k after sorting nums in descending order.

Examples

Example 1

Input: ([5,-3,9,1], 0)
Output: 9

Explanation: k=0 is largest.

Example 2

Input: ([5,-3,9,1], 3)
Output: -3

Explanation: k=3 is smallest in four elements.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.

Longest Vacation With PTO

Return the longest contiguous block containing at most pto workdays, where holidays and covered workdays count as vacation.

Examples

Example 1

Input: ('WHHWWHW', 2)
Output: 5

Explanation: Use PTO to create a length-5 vacation block.

Example 2

Input: ('WWW', 1)
Output: 1

Explanation: Only one workday can be covered.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.