Top K Numbers

In this blog post, I will be giving a brief introduction on another important data structure pattern that is very important for technical interviews.This pattern is called Top K Numbers. This pattern is very useful to solve the problems that asks to find the top/smallest/frequent K elements in a given set.

The best data structure to keep track of K elements is Heap. The pattern looks like this;

  1. Based on the problem, insert ‘K’ elements into the min-heap or max-heap.
  2. Iterate through the remaining numbers, and if you find the number that is larger than what you have in the heap, then remove that number and insert the larger one.
source: hackernoon.com

Time Complexity: O(N log K).

Space Complexity: O(K)

References:

--

--

--

Student at Flatiron School

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CRODO VS BinStarter: Battle of the Launchpads

Coding for a game is easy

Tutorial Fuzzy Logic Mamdani for Arduino

Tutorial Fuzzy Logic Mamdani for Arduino

Browser Automation with Python and Selenium — 11: Working with Cookies

Enumerable Ruby

Browser Automation with Python and Selenium — 18: Remote WebDriver

Code Ninjas Lubbock Summer Camp Schedule

A Brief History of the Curly Brace in Programming

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
anil gurung

anil gurung

Student at Flatiron School

More from Medium

My Journey With Software Development.

Bootcamp week 12: Certified dev

What is Big O Notation? And why do I need to know it to get a Software Engineer job?

Journey to software programming mastery!