Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Heaps & priority queues

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Heaps & priority queues

Kth Largest Element in an Array

intermediatelayer I3 · Heaps & priority queues5 tests

Return the k-th largest value in nums. This is the k-th in sorted order, not the k-th distinct value — in [3,2,3,1,2,4,5,5,6] the 4th largest is 4.

  • A MinHeap class is already written for you in the starter — you only need the algorithm
  • The heap route keeps the k best seen so far and runs in O(n log k)
  • Quickselect solves it in O(n) on average by partitioning around a pivot and recursing into one side only — either approach passes
  • 1 <= k <= nums.length, and duplicates are allowed

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen