PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

DS with insert, delete and getRandomElement in O(1)

(1 votes, average: 5.00 out of 5)

July 5, 2013 by puzzler Leave a Comment

Problem Statement

Design a data structure that allows insert, delete and getRandomElement() 
in constant time i.e. O(1).

Solution

Insert and delete in O(1) needs a hashmap and get random element on O(1) suggests some kind of array access.

We can take a HashMap H and an array A with max size. Now HashMap should have key as the element to be inserted and value as the position of the element in the array, also keep a last pointer telling the index of the last element in the array.

Insert is simple, increment last pointer and add the new element at last position and add <element, last> in the hashmap.

Delete is a little tricky, First get the position of element in the array from hashmap, remove the element from hashmap, swap A[pos] and A[last] in array, decrement last pointer and update the position of swapped element in the hashmap.

delete(element){
  int pos =  map.get(element);
  map.remove(element);
  Swap A[pos], A[last];
  last--;
  map.put(A[pos], pos);//update position to new pos
}

getRandomElement() is simple, get a random number between 0 and last(both including) and return A[random number]

  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
Output of the recursive program

Checkout more Interview Questions Tags: Data Structures, Solved Puzzles

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • Swap two integer variables
  • Objective Questions set C/C++ – Part 3
  • Spinning Disc Direction Puzzle
  • Consonants Solution: Google codejam 2013 Round 1C
  • Deceitful War Solution – Google Code Jam 2014
  • Objective Questions set C/C++ – Part 2
  • Bullseye Solution: Google codejam 2013 Round 1A
  • Output of the recursive program
  • Osmos Solution: Google codejam 2013 Round 1B
  • Cookie Clicker Alpha Solution – Google Code Jam 2014

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous