Menu
Tech Interview Handbook LogoTech Interview Handbook
Getting StartedBlogGitHub
Tech Interview Handbook LogoTech Interview Handbook
  • Getting Started
  • Blog
  • GitHub
  • Preface
    • Introduction
    • Landscape
  • Job Application
    • Resume
    • Cover Letter
  • Interview Process
    • Interview Formats
    • Self Introduction
    • Coding Round
      • Overview
      • Picking a Language
      • Study and Practice
      • Best Practice Questions
      • During the Coding Interview
      • Cheatsheet
      • Coding Signals
      • Mock Interviews
      • Cheatsheet (Interviewer)
    • Behavioral Round
      • Overview
      • STAR Format
      • Common Questions
      • Psychological Tricks
    • Questions To Ask
  • Post Interview
    • Understanding Compensation
    • Negotiation
  • Algorithms
    • Introduction
    • Array
    • Binary
    • Dynamic Programming
    • Geometry
    • Graph
    • Hash Table
    • Heap
    • Interval
    • Linked List
    • Math
    • Matrix
    • Object-Oriented Programming
    • Permutation
    • Queue
    • Recursion
    • Sorting and Searching
    • Stack
    • String
    • Tree
    • Trie

Heap

Study links#

  • Learning to Love Heaps

Notes#

If you see a top or lowest k being mentioned in the question, it is usually a signal that a heap can be used to solve the problem, such as in Top K Frequent Elements.

If you require the top k elements use a Min Heap of size k. Iterate through each element, pushing it into the heap. Whenever the heap size exceeds k, remove the minimum element, that will guarantee that you have the k largest elements.

Recommended LeetCode questions#

  • Merge K Sorted Lists
  • Top K Frequent Elements
  • Find Median from Data Stream
Edit this page
Last updated on 9/26/2019 by Yangshun Tay
Previous
« Hash Table
Next
Interval »
  • Study links
  • Notes
  • Recommended LeetCode questions
Copyright © 2020 Yangshun Tay. Built with Docusaurus.