Day 79 — Insert, Delete, Random O(1)🏁
By: House Of Codes
Tags: Software Development, Coding, Interview, Java, Programming

Day 79 — Insert, Delete, Random O(1)

100 Days to Amazon

Photo by Kaze 0421 on Unsplash

100 Days to Amazon — Day 79 Insert, Delete, Random O(1)

Out of Free Stories? Here is my Friend Link.

Introduction🛹

Hey Guys, Today is day 79 of the challenge that I took. Wherein I will be solving every day for 100 days the programming questions that have been asked in previous interviews.

You have a bonus at the end if you keep reading. You can find out the companies that have asked these questions in real interviews.

All these problems are taken from the following e-book. 🎓

This is completely free 🆓 if you have an amazon kindle subscription.

This e-book contains 100 coding problems that have been asked in top tech interview questions. It also has a guide to solving all the problems in 200+ ways. These problems I assure you has been asked in previous interviews.

You have to decide whether you want to go unprepared for a tech interview or go ahead and quickly search for this guide to solve the 100 problems.

Begin Your ascent to greatness🚀

Note: this e-book only contains the links to the solutions. Code for 40 have been added.

Day 79 — Insert, Delete, Random O(1)🏁

AIM🏹

Design a data structure that supports all following operations in average O(1) time.

insert(val) : Inserts an item val to the set if not already present. remove(val) : Removes an item val from the set if present. getRandom : Returns a random element from current set of elements. Each element must have the same probability of being returned.

Example🕶

// Inserts 1 to the set. Returns true as 1 was inserted successfully.

randomSet.insert(1);



// Returns false as 2 does not exist in the set.

randomSet.remove(2);



// Inserts 2 to the set, returns true. Set now contains [1,2].

randomSet.insert(2);



// getRandom should return either 1 or 2 randomly.

randomSet.getRandom();



// Removes 1 from the set, returns true. Set now contains [2].

randomSet.remove(1);



// 2 was already in the set, so return false.

randomSet.insert(2);



// Since 2 is the only number in the set, getRandom always return 2.

randomSet.getRandom();

Code👇

Author: Akshay Ravindran

Algorithm👨‍🎓

Use HashMap for retrieval and insertion in O(1). When removing we could directly remove from the hashmap. But due to the increase and decrease of the list. A hashmap won’t be able to remember these indexes. To solve this you have to introduce the ArrayList. This List keeps track of the changes in indexes and the insertion position. Use Random Class to Create a Random object. This random object if given the size of an input array will return the index within the range based on equal probability. Return the values as expected based on the functions.🔚

Conclusion🐱‍🏍

Have you come across this question in your interview before? Share it in the comment section below. 🤝

Don’t forget to hit the follow button✅to receive updates when we post new coding challenges. Tell us how you solved this problem. 🔥 We would be thrilled to read them. ❤ We can feature your method in one of the blog posts.

Want to become outstanding in java programming?

Click HERE 🧨🎊🎃

I have published an ebook. A compilation of 100 Java(Interview) Programming problems which have been solved. (HackerRank) 🐱‍💻

This is completely free 🆓 if you have an amazon kindle subscription.

Companies

Previous Blog Posts