• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Get K Max and Delete K Max in stream of incoming integers

March 25, 2014 by Dhaval Dave

Get K Max and Delete K Max in stream of Incoming Integers

Solution 1 : Using Min Heap
Algorithm :

1) Keep a Min & Max Heap of K size
2) Store first K elements in them.
3) If a new element comes with smaller or larger than root then reorder   heap.

Example : 

Min heap Max heap

Complexity :
-O(1) for search First Minimum/Maximum.
-To get second Min/Max we need to Delete Min/Max element reordering takes O(log N) and again we can Find second Min/Max in O(1).
– To insert new Min/Max element takes O(log N)

Solution 2: Using Linked List or Queue.

Example :

Algorithm

Whenever new number x comes, check
if x < head -> data than  (in case of 0 here)
   create node Temp
   Temp->next = head
   Temp = Head.
   remove rear (to keep K size constant)
else of  x < rear -> data than (in case of 2 here)
   create new Temp node
   traverse link list and put Temp at appropriate node (insert)
   remove rear (to keep K size constant)

Same solution can be applied for K Max

Time Complexity
O(1) To get Min/Max
O(K) To get K Min/Max
O(K) To reorder at each time.

-IF YOU LIKE OUR EFFORT, PLEASE LIKE AND SHARE OUR FB-PAGE AND SITE

Similar Articles

Filed Under: Adobe Interview Questions, Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Linked List, Max Heap, Min Heap

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Find position of the only set bit

Closed Parentheses checker

LeetCode : Word Search

1014 Practice Question of New GRE – Princeton

Maximum occurred Smallest integer in n ranges

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

DFS (Depth First Search)

Check Binary Tree is Binary Search Tree or not

Regular Expression Matching

flattens 2 D linked list to a single sorted link list

Test Cases for Round Function

Maximum difference between two elements s.t larger element appears after the smaller number

Serialise Deserialise N-ary Tree

Singly linked list

Find the smallest window in a string containing all characters of another string

Amazon Interview Experience – SDE Chennai

Count number of ways to reach a given score in a game

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Minimum insertions to form a palindrome

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Spanning Tree

Sort an array according to the order defined by another array

Generic Object Oriented Stack with Template

Advanced SQL Injection

Maximum of all subarrays of size k

Best Java Book | Top Java Programming Book for Beginners

Interfaces in C++ (Abstract Classes in C++)

Introduction To Number Theory ( Part 1 )

Doubly linked list

Knight Tour Problem (Graph – Breadth First Search)

Copyright © 2026 · Genesis Framework · WordPress · Log in