• 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

Microsoft BING Interview Experience

ADOBE Aptitude C Language Test

Flipkart Set 1 On Campus with Answers

Knight Tour Problem (Graph – Breadth First Search)

Singly linked list

Check if an array has duplicate numbers in O(n) time and O(1) space

Generate next palindrome number

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Difference between a LinkedList and a Binary Search Tree BST

SAP Off Campus Hiring_ March 2015 Computer Skills

Cisco Hiring Event 21st – 22nd Feb 2015

Coin Collection Dynamic Programming

Maximum occurred Smallest integer in n ranges

Spanning Tree

Minimum insertions to form a palindrome

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

BlueStone E-commerce Interview Experience

Convert number to words java

Generate largest number arranging a no. of given non negative integer numbers

Find position of the only set bit

Adobe Interview Questions 8 month Exp

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

Binary Tree in Java

Serialise Deserialise N-ary Tree

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

Walmart Labs Interview Experience

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Wrong Directions given find minimum moves so that he can reach to the destination

Skiing on Mountains Matrix

Copyright © 2026 · Genesis Framework · WordPress · Log in