Data structure: insert, remove, contains, get random element, all at O(1)

后端 未结 14 732
别跟我提以往
别跟我提以往 2020-11-29 14:53

I was given this problem in an interview. How would you have answered?

Design a data structure that offers the following operations in O(1) time:

  • inse
14条回答
  •  我在风中等你
    2020-11-29 15:22

    For this Question i will use two Data Structure

    • HashMap
    • ArrayList / Array / Double LinkedList.

    Steps :-

    1. Insertion :- Check if X is already present in HashMap --Time complexity O(1) . if not Present Then Add in end of ArrayList -- Time complexity O(1). add it in HashMap also x as key and last Index as a value -- Time complexity O(1).
    2. Remove :- Check if X is present in HashMap --Time complexity O(1). If present then find the its index and remove it from HashMap --Time complexity O(1). swap this element with last element in ArrayList and remove the last element --Time complexity O(1). Update the index of last Element in HashMap --Time complexity O(1).
    3. GetRandom :- Generate Random number from 0 to last index of ArrayList . return the ArrayList element at random index generated --Time complexity O(1).
    4. Search :- See in HashMap for x as a key. --Time complexity O(1).

    Code :-

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class JavaApplication1 {
    
        public static void main(String args[]){
           Scanner sc = new Scanner(System.in);
            ArrayList al =new ArrayList();
            HashMap mp = new HashMap();  
            while(true){
                System.out.println("**menu**");
                System.out.println("1.insert");
                System.out.println("2.remove");
                System.out.println("3.search");
                System.out.println("4.rendom");
                int ch = sc.nextInt();
                switch(ch){
                    case 1 : System.out.println("Enter the Element ");
                            int a = sc.nextInt();
                            if(mp.containsKey(a)){
                                System.out.println("Element is already present ");
                            }
                            else{
                                al.add(a);
                                mp.put(a, al.size()-1);
    
                            }
                            break;
                    case 2 : System.out.println("Enter the Element Which u want to remove");
                            a = sc.nextInt();
                            if(mp.containsKey(a)){
    
                                int size = al.size();
                                int index = mp.get(a);
    
                                int last = al.get(size-1);
                                Collections.swap(al, index,  size-1);
    
                                al.remove(size-1);
                                mp.put(last, index);
    
                                System.out.println("Data Deleted");
    
                            }
                            else{
                                System.out.println("Data Not found");
                            }
                            break;
                    case 3 : System.out.println("Enter the Element to Search");
                            a = sc.nextInt();
                            if(mp.containsKey(a)){
                                System.out.println(mp.get(a));
                            }
                            else{
                                System.out.println("Data Not Found");
                            }
                            break;
                    case 4 : Random rm = new Random();
                            int index = rm.nextInt(al.size());
                            System.out.println(al.get(index));
                            break;
    
                }
            }
        }
    
    }
    

    -- Time complexity O(1). -- Space complexity O(N).

提交回复
热议问题