PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates skills in array manipulation, in-place algorithm design, numerical stability and edge-case reasoning (such as handling multiple zeros and large products), plus selection and ranking competencies for extracting the top-k elements.

  • medium
  • Amazon
  • Coding & Algorithms
  • Machine Learning Engineer

Compute array products excluding self and top-k

Company: Amazon

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Algorithms ### 1) Product of array except self (no division) Given an integer array `nums` of length `n`, return an array `ans` where: - `ans[i]` = product of all `nums[j]` for `j != i` - You **must not use division** - Target time: **O(n)** - Extra space: **O(1)** beyond the output array #### Follow-ups - How does your approach handle **many zeros** in the input? - How do you handle **overflow / very large products**? ### 2) Find the k largest elements Given an unsorted array of numbers and an integer `k`, return the **k largest elements** (order does not matter unless specified). Discuss time/space trade-offs for different approaches.

Quick Answer: This question evaluates skills in array manipulation, in-place algorithm design, numerical stability and edge-case reasoning (such as handling multiple zeros and large products), plus selection and ranking competencies for extracting the top-k elements.

Product of Array Except Self

Return ans where ans[i] is the product of every nums[j] for j != i, without using division.

Constraints

  • Do not use division
  • O(1) extra space beyond output

Examples

Input: ([1, 2, 3, 4],)

Expected Output: [24, 12, 8, 6]

Input: ([0, 1, 2, 3],)

Expected Output: [6, 0, 0, 0]

Input: ([0, 0, 2],)

Expected Output: [0, 0, 0]

Input: ([-1, 2, -3],)

Expected Output: [-6, 3, -2]

Hints

  1. Store prefix products in the output, then multiply by suffix products.

K Largest Elements

Return the k largest elements from nums in descending order. If k exceeds len(nums), return all elements sorted descending.

Constraints

  • k may be 0 or larger than n

Examples

Input: ([3, 1, 5, 2, 5], 3)

Expected Output: [5, 5, 3]

Input: ([1], 5)

Expected Output: [1]

Input: ([4, 4, 4], 2)

Expected Output: [4, 4]

Input: ([9, 8], 0)

Expected Output: []

Hints

  1. A heap works well when k is much smaller than n.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.

Related Coding Questions

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)