Open In App

Palantir Technologies Interview | Set 1

Last Updated : 05 May, 2015
Improve
Improve
Like Article
Like
Save
Share
Report

The interview was scheduled after I passed the coding challenge.

The coding challenge was kinda simple, it was a grid, with each cell connected to its neighbors with some cost, you are allowed to move right and down, and you are required to calculate the minimum total cost for some employees (defined on the grid) to reach the bottom corner.
The grid size was a max of 1000*1000, so a simple Dijkstra could solve it.

The interview started about 20 minutes late due to technical problems with Skype connection, so the interviewer dived in quickly to the technical question.

So the technical question was as follows:

We want to create LRU Cache, a data structure that stores pairs , and has maximum capacity, after which, any insertion process should remove the least recently used element.
An element is considered used when it’s first inserted to the cache, and if its value was retrieved.

The interviewer wrote the body of the class, and my task was to implement it.
The first idea I got was to use a doubly-linked list, where any insertion process to the cache means we insert a new element as the head of the linked list, and of course the least recently used element is at the tail of the list.

The insertion worst case run-time was O(1), but the retrieval method was to run in O(n).

I was then asked to find a better way to improve the running time of the get method, and I suggested we use a hashtable to store the pairs , but then we would still need another way to store the order of usage of every element.

I suggested we can use a heap, to easily retrieve the least recently used element, but the interviewer asked me to combine the idea of the hash table with the idea of the linked list to get a better implementation.
So, the idea I got was to create both a hash table to be used in the get method, and a linked list to store the precedence of each key.
So, for each key, we have 2 entries, 1 entry in the hash table, and another one in the linked list, so I created another hash table, that given the key, it returns the node of that key in the linked list.

I started writing the code and kept updating it whenever we find a bug.

My final code was as follows:




// This is the text editor interface. 
// Anything you type or change here will be seen by the other 
// person in real time.
  
/*
 *  Cache of at most maxCapacity objects, referenced by identifiers of 
 *  type <K>.  When the cache needs to free up space, it drops the 
 *  least-recently-used object.
 */
class LruCache<K, T> {
      
    class ListNode
    {
        public K key;
        public ListNode next, prev;
        public ListNode()
        {
            next = prev = null;
        }
        public ListNode (K key)
        {
            this.key = key;
        }
        public ListNode (ListNode next, ListNode prev)
        {
            this.next = next;
            this.prev = prev;
        }
        public setNext (ListNode next)
        public getNext
        public setPrev (ListNode prev)
        public getPrev
    }
      
    public class LinkedList
    {
        public ListNode head, tail;
        public int size;
        public LinkedList ()
        {
            head = tail = null;
            size = 0;
        }
        public LinkedList (ListNode node)
        {
            this.head = this.tail = node;
        }
          
        public void removeNode (ListNode node)
        {
            if (node != list.getHead())
            {
                ListNode prev = node.prev;
                ListNode next = node.next;
                if (prev != null)
                    prev.setNext(next);
                if (next != null)
                    next.setPrev(prev);
                else
                    list.setTail (prev);
            }
            else
            {
                if (head == tail)
                    head = tail = null;
                else
                    head = head.next;
            }
        }
        public void setHead (ListNode node)
        {
            if (head != null)
                head.setPrev = node;
            node.next = head;
            node.prev = null;
            head = node;
        }
        public ListNode getHead
        public void setTail (ListNode node)
        {
            if (tail != null)
            {
                node.prev = tail;
                tail.next = node;
            }
            tail = node;
            node.next = null;
        }
        public ListNode getTail
    }
      
    /*
    --------------------- (K1, T1) <- 1 (1, K1)        (LRU)
    --------------------- (K2, T2) <- 2 (2, K2)
    (K3, T3) <- 3 (3, K3)       (MRU)
    */
      
    private final int maxCapacity;
    private HashTable <K, T> valueTable;
    private HashTable <K, ListNode> nodeTable;
    private LinkedList list;
    /*
     * Returns object with identifier <key>, stored in the cache.  
     * This object then becomes most-recently-used.
     */
    public T get(K key) {
        // fill in
        if (!valueTable.contains(key))
            return null;
        ListNode node = nodeTable.get(key);
        list.removeNode(node);
        list.setHead(node);
        return valueTable.get(key);
    }
      
    /*
     * Puts <object> in the cache.  At the time it's put in 
     * it is the most-recently-used.
     */
    public void put(K key, T object) {
        // fill in
        ListNode newNode = new ListNode (key);
        list.setHead = newNode;
        list.size++;
        valueTable.add(key, object);
        nodeTable.add(key, newNode);
        if (list.size > maxCapacity)
        {
            ListNode tail = list.getTail();
            valueTable.remove (tail.getKey());
            nodeTable.remove (tail.getKey());
            list.removeNode (tail);
            list.size--;
        }
    }    
}


After we finished the technical question, the interviewer moved on to discuss the projects on my resume, and asked which project was most enjoyable and why.
My answer was for the Ocean Simulation project, because among all projects I have done in college, that was the one I most used algorithms and tried to make the software as smart as possible.

1 thing is, I asked the interviewer about interns life at Palantir, his reply was “Are you applying for an internship?”



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads