• 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 if a binary tree is height balanced ?

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Serialise Deserialise N-ary Tree

Subset Sum Problem Dynamic programming

Search element in a matrix with all rows and columns in sorted order

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Possible sizes of bus to carry n groups of friends

Python List

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Find Percentage of Words matching in Two Strings

Given a float number convert it into the string WITHOUT using any inbuilt Function

Find the kth number with prime factors 3, 5 and 7

How strtok() Works

Stickler thief

Reliance Jio Software Developer Interview Experience

Find Nearest Minimum number in left side in O(n)

Find and print longest consecutive number sequence in a given sequence in O(n)

Print vertical sum of all the axis in the given binary tree

Maximum size of square sub matrix with all 1’s in a binary matrix

Circular Linked List

LeetCode: Container With Most Water

Longest Increasing Subsequence

Code Chef PRGIFT Solution

Walmart Labs Interview Experience

Find min element in Sorted Rotated Array (Without Duplicates)

Microsoft BING Interview Experience

Find next greater number with same set of digits

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

VMWare SDEII Interview

Copyright © 2025 · Genesis Framework · WordPress · Log in