Twitter Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

Use ReadWriteLock

static class ThreadSafeHashMap<K, V> extends HashMap {

private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

private final Lock read = readWriteLock.readLock();

private final Lock write = readWriteLock.writeLock();



public void put(K key, V value) {
write.lock();
try {
super.put(key, value);
} finally {
write.unlock();
}
}



public Object get(K key) {
read.lock();
try {
return super.get(key);
} finally {
read.unlock();
}
}
...

}

- Miz March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the question says "support concurrent read"? Isn't this locking on read as well?

- Monad January 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry my bad. I see why now.

- Monad January 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We could talk about granularity of locking: whole hash table vs. each hash buckets vs. set of buckets. Also, we don't have to lock readers from other readers -- multi reader single writer lock.

- Sankaran June 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a classic problem of multiple readers and single writer.
But you can refer to code for given in operating systems: concurrent and distributed software design by Jean Bacon and Tim Harris

- googlybhai March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Hashtable
{
private int size = 100000;
pair[] slot;
Lock[] write_lock;
int currentSize;

class pair
{
K key;
V value;
}
public Hashtable()
{
value = new V[size];
write_lock = new Lock[size];
currentSize = 0;
}

private int hashcode(K key, S preSlot){}

public V put(K key, V value)
{
int index= -1;
do
{
index = hashcode(K, index);
if(slot[index]==null)
{
write_lock[index].lock();
if(slot[index]==null)
{
slot[index] = new pair();
slot[index].key = key;
slot[index].value = value;
return null;
}
else if(slot[index].key==key)
{
V preValue = null;
preValue = slot[index].value;
slot[index].value = value;
return preValue;
}
write_lock[index].unlock();
}
else
{
if(slot[index].key==key)
{
V preValue = null;
preValue = slot[index].value;
slot[index].value = value;
return preValue;
}
}

}while(currentSize < size*0.5);

this.resize();
return put(key, value);
}
}

- LoocalVinci October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Class ReaderWriterLock()
{
    // setup internal useful variables
    volatile int readLock = 0;
    volatile bool writeLock;    
    object synch = new Object();

    // Block until lock is free
    //ReadLock
    void ReadLock()
    {
     Lock (synch)
     {
         while (writeLock == true)
             Thread.Sleep(1); // 1 second
         readLock++;                 
     }
    }

        // Block until lock is free
    //Write
    void WriteLock()
    {
        Lock (synch)
        {
         while (writeLock == true || readLock>0)
             Thread.Sleep(1); // 1 second
         writeLock = true;                 
         }
    }
 
    void ReleaseWriteLock()
    {
        Lock (synch)
        {
            Assert (writeLock == true);    
            wrtieLock = false;
        }
    }
    void ReleaseReadLock()
    {
        Lock (synch)
        {
            Assert (readLock > 0);    
            readLock--;
        }
    }
}

- Anonymous December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Why not use ConcurrentHashMap ?

- Anonymous September 15, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More