Java solution extends LinkedHashMap , is it allowed?


  • 0
    W
    	public class LRUCache<K,V> extends LinkedHashMap<K,V>{
    	public int maxCapacity;
    	public LRUCache(Integer capacity){
    		super(capacity,1,true);
    		maxCapacity=capacity;
    
    	}
        @Override  
        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {  
            if(size() > this.maxCapacity) {  
                return true;  
            }  
            return false;  
        } 
    	public int get(int key) {
    	    return this.containsKey(key)? (Integer)super.get(key):-1;
    	}
    	public void set(K key, V value) {
    	    super.put(key,value);
    	}
    

    }


  • 0
    A

    In real-world cases I think it is, but I wouldn't dare offering a solution with extend on an interview (except if it was expelicitly asked).

    As far as I understood it, the point here is to demonstrate your algorithm-designing and data structure designing skills, and by extending an almost-complete solution, your interviewer will have less opportunity to see those skills of yours.

    On the other hand you can mention, that solving the problem would be easier just by extending LinkedHashMap (However I'd also mention, that by that, most of the signatures would be messed up - generics -, so if the class was used previously, the codebase using it requires refactoring).


  • 0
    K

    I think this is the best Java solution, because a) you should know your standard library, and b) do not reimplement unnecessarily.

    Unfortunately, they deliberately do not auto #include LinkedHashMap, and additional #include statements are forbidden. I find this disappointing.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.