Google Interview Question for Developer Program Engineers






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

Sorry in my code above AllowOne needs a mutex as well:

void AllowOne()
{
   mutex.Acquire();
   int n = 0;
   while (n < semaphore_count)
   {
      semaphore.Acquire(); // This shall lock all threads from accessing AllowMany
   }
   mutex.Release();

   // Critical section
   while (n > 0)
   {
      semaphore.Release();
   }   
}

This is so because multiple threads can also access AllowOne at the same time.

- Ashish Kaila August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You also need use mutex in AllowMany function.

- Jack August 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think the given question exposes the semaphore_count variable semaphore_count.

- Anonymous March 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

void AllowMany() {
// in the time I'm doing allow one I do not want anybody getting in, so I'll wait here
// otherwise just aquire and release
mutex.Acquire();
mutex.Release();

semaphore.Acquire();
...allowmany code comes here
semaphore.Release();
}

void AllowOne()
{
mutex.Acquire();
// first wait everybody to get out of the allow many because I don't want to //release something that was aquired by the aloow many and vice versa

while (semaphore_count>0)
Thread.wait(100);

// now my critical section, I now that nobody is in allow many and nobody can get in


mutex.Release();

}

- dav August 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the method , where more than one thread can be allowed , use semaphore for sync ..

fun1() // where only one thread is allowed..
{
Createthread // single thread ..

}

fun2()
{

{
semaphore obj(count);
// create number of threads
createthread();
}
}

criticalsectioncode()
{
waitforsingleobject(mutexhandle)
waitformultipleobjects(semaphore)
}

Threadproc1 // for mutex
{
// access critical section code
}
Threadproc2 // forsemaphore
{
// access critical section code
}

- Anonymous August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are you looking to get your homework done?

One very similar sound question (in fact same paragraph) was just posted recently as an Amazon question!

- Anonymous August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Seems to me like a reader writer problem. Second method is writer equivalent, first method is reader equivalent.

void AllowMany()
{
   semaphore.Acquire();
   // Critical section
   semaphore.Release();
}

void AllowOne()
{
   int n = 0;
   while (n < semaphore_count)
   {
      semaphore.Acquire(); // This shall lock all threads from accessing AllowMany
   }
   // Critical section
   while (n > 0)
   {
      semaphore.Release();
   }   
}

Note the two loops in AllowOne. This ensures there is no starvation between readers and writers. In essence you won't need to use a mutex !

- Ashish Kaila August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

While you are trying to acquire all counts in AllowOne, callers to AllowMany can still acquire so there is a race condition in your solution.

- Anonymous August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No it is not a race condition but it removes starvation. Threads coming into AllowMany would not be pre-empted by AllowOne and priority of semaphore acquisition will remain the same.

- Ashish Kaila August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

MAX_READERS = 32;
Semaphore semaphore = new Semaphore(MAX_READERS);
    
void Read() {
        ...
        semaphore.acquire();
        read();
        semaphore.release();
        ...
    }

void Write() {
        ...
        mutex.lock();
        for (int i = 0; i < MAX_READERS; ++i)
            semaphore.acquire();
        mutex.unlock();
        write();
        semaphore.release(MAX_READERS);
        ...
    }

- philippppe September 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

AllowOne is waiting on the mutex when all the AllowMany get out and vice versa.

I call it read write mutex because it is similar to the problem when you allow many readers but only writer to access the critical section. You can use it in the provided class or any place you'd find a similar issue

class ReadWriteMutex
{
    public:
    ReadWriteMutex(unsigned readers):
    maxreaders(readers), s(maxreaders), nreaders(0),writeRequest(FALSE)
    {


    };
    void ReadAccess()
    {
   
        while(1)
        {
            m_internal.Aquire();
            unsigne t = writeRequest;
            if (!writeRequest)
            {

                if ( nreaders++ == 0)//first reader comes
                    m_read.Acquire();
                m_internal.Release()
                break;
            }
            m_internal.Release();
            if (t)
            {
               m_read.Acquire();
               m_read.Release();
            }
        }
        {
            s.Acquire();
        }
    }
    void WriteAccess()
    {
        while(1)
        {
            m_internal.Aquire();
            writeRequest = TRUE;
            unsigned n = nreaders;
            if (!nreaders) //no mo readers - the CS is exclusively locked for the writer.
            {
                m_read.Acquire();
                m_internal.Release();
                break;
            }
            m_internal.Release();
            if (n)
            {
               m_read.Acquire();
               m_read.Release();
            }

        }
    }
    void ReadExit()
    {
        m_internal.Aquire();
        s.Release();
        nreaders--;
        if (!nreaders)
            m_read.Release();
        m_internal.Release();
    }
    void WriteExit()
    {
       m_internal.Aquire();
       writeRequest = FALSE;
       m_read.Release();
       m_internal.Release();

    }


    private:
    unsigned maxreaders;
    Semaphore s;
    Mutex   m_read,
            m_internal;
    unsigned nreaders;
    bool writeRequest;

};

- kykydyk March 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you should also explain how does your answer fit into question being asked.

- rahul.s July 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would do this way, though i feel this might not be correct. semaphores are used to signal conditions

class ClassThatNeedsFixing
{
	Mutex 		m;
	Semaphore 	s;
	int			count;
	
	public:
	ClassThatNeedsFixing(int count): m(), s(count), count(count)
	{}

	void AllowMany(void)
	{
		m.acquire();
		s.acquire();
		m.release();
		
		// whatever you wanna do
		s.release();
	}

	void AllowOne(void)
	{
		m.acquire();
		int n = count;
		while(n > 0 )
		{
			s.acquire();
			n--;
		}
	
		// i am gonna block all

		n = count;
		while(n>0)
		{
			s.release();
			n--;
		}
		m.release();
	}

}

- confused_banda November 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Why we can just use ReadWriteLock?

- max August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Why we can't just use ReadWriteLock?

- max August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

becuase u didn't read the question. u have to do it with mutex and semaphore dude.

- Anonymous September 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And who says ReadWriteLock cannot be implemented using mutex and semaphore?

- Anonymous November 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

// Using semaphone and mutex in AllowMany function and Using mutex in AllowOne function
void AllowMany() {
if(semaphore_count == 0) mutex.Acquire();
semaphore_count ++;

// Here is the critical section that must be protected

semaphore_count ++;
if(semaphore_count == 0) mutex.Release();
}
 
// Will lock out any client, including callers to the other method.
void AllowOne() {
mutex.Acquire();

// Here is the critical section that must be protected

mutex.Release();
}

- zombie August 21, 2011 | 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