• 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 min element in Sorted Rotated Array (With Duplicates)

25 horses 5 tracks Find 3 fastest puzzle

Maximum path sum between two leaves

Spanning Tree

HackeEarth Flipkart’s Drone

BFS (Breath First Search)

Generic Object Oriented Stack with Template

Printing Longest Common Subsequence

BlueStone E-commerce Interview Experience

Find position of the only set bit

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Leetcode: Merge Intervals

Python Dictionaries

Walmart Labs Interview Experience

Python String and numbers

How Radix sort works

Given a string, find the first character which is non-repetitive

How strtok() Works

Handle duplicates in Binary Search Tree

flattens 2 D linked list to a single sorted link list

Daughter’s Age VeryGood Puzzle

Find the element that appears once others appears thrice

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

Count Possible Decodings of a given Digit Sequence

VMWare Openings

SAP Off Campus Hiring_ March 2015 Verbal Skills

LeetCode: Container With Most Water

Maximum sum contiguous subarray of an Array

Edit Distance ( Dynamic Programming )

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

Copyright © 2026 · Genesis Framework · WordPress · Log in