PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers
|Home/Coding & Algorithms/Amazon

Solve subset-count and kth-factor problems

Last updated: Mar 29, 2026

Quick Overview

This question evaluates algorithm design and analysis skills, including selection under sum constraints with negatives and zeros, streaming and query-preprocessing strategies for repeated budgets, and number-theoretic factor enumeration for large N, all within the Coding & Algorithms domain.

  • hard
  • Amazon
  • Coding & Algorithms
  • Data Scientist

Solve subset-count and kth-factor problems

Company: Amazon

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

You will solve two independent tasks: A) Maximum count under sum constraint: Given an array of integers arr and an integer n, return the maximum number of elements you can choose such that their sum ≤ n. Output only the count. Constraints: 1 ≤ |arr| ≤ 200000, −1e9 ≤ arr[i] ≤ 1e9, and n fits in 64-bit signed integer. Requirements: design an algorithm that is optimal for the case where all arr[i] ≥ 0; handle zeros and negative numbers correctly; analyze time and space complexity; prove correctness for the non‑negative case; and describe how you would support Q up to 100000 queries with different n on the same static arr. Follow‑ups: What changes if arr arrives as a stream you cannot fully store? Provide a strategy with at most ±1 error in the count and justify it. B) Find the Kth factor: Given positive integers N and K (1‑indexed), return the Kth factor of N in ascending order; if fewer than K factors exist, return −1. Optimize for N up to 10^12. Discuss an O(√N) solution that avoids duplicate factors when N is a perfect square and analyze time/space trade‑offs.

Quick Answer: This question evaluates algorithm design and analysis skills, including selection under sum constraints with negatives and zeros, streaming and query-preprocessing strategies for repeated budgets, and number-theoretic factor enumeration for large N, all within the Coding & Algorithms domain.

Related Interview Questions

  • Find Valid IP Addresses in Files - Amazon (medium)
  • Implement Optimal Bucket Batching - Amazon (hard)
  • Implement Cache and Rotate Matrix - Amazon (medium)
  • Find Longest Activatable Server Streak - Amazon (hard)
  • Build the Largest Available Sequence - Amazon (medium)
Amazon logo
Amazon
Oct 13, 2025, 9:49 PM
Data Scientist
Take-home Project
Coding & Algorithms
2
0

Two Independent Tasks

A) Maximum Count Under Sum Constraint

You are given an integer array arr and an integer budget n. Return the maximum number of elements you can select whose sum is ≤ n. Output only the count.

  • Constraints:
    • 1 ≤ |arr| ≤ 200,000
    • −1e9 ≤ arr[i] ≤ 1e9
    • n fits in a 64-bit signed integer
  • Requirements:
    1. Design an algorithm that is optimal for the case arr[i] ≥ 0.
    2. Handle zeros and negative numbers correctly.
    3. Analyze time and space complexity.
    4. Prove correctness for the non‑negative case.
    5. Describe how to answer up to Q = 100,000 queries with different n on the same static arr.
  • Follow‑ups:
    • If arr arrives as a stream you cannot fully store, what changes?
    • Provide a strategy with at most ±1 error in the count and justify it.

B) Find the Kth Factor

Given positive integers N and K (1‑indexed), return the Kth factor of N in ascending order; if fewer than K factors exist, return −1.

  • Optimize for N up to 10^12.
  • Discuss an O(√N) solution that avoids duplicate factors when N is a perfect square and analyze time/space trade‑offs.

Solution

Show

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Data Scientist•Amazon Data Scientist•Amazon Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.