Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Take two pointers, read and write.
Read points to the next read index
Write points to the next write index

class Ring
{
    string buffer;
    int n, r, w;    // size of ring buffer, read pointer, write pointer

public:

    Ring(int _n) : n(_n)
    {   r = w = 0;  }

    string Read(size_t sz)
    {
        string chunk;

        while (r != w && chunk.length() < sz)
        {
            chunk += buffer[r++];

            if (r == n)
                r = 0;
        }

        return chunk;
    }

    size_t Write(string chunk)
    {
        int i;

        for (i=0; i<chunk.length(); i++)
        {
            if ((w+1) % n == r)
                break;

            buffer[w++] = chunk[i];
            
            if  (w==n)
                w = 0;
        }
        
        return i;
    }
};

- sheva July 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class RingBuffer {
	public:
		RingBuffer(int size)
		{
			if (size > 0) {
				// adjust size to support the read/write pointers overflow
				int pow = 0;
				while (size != 0) {
					++pow;
					size >>= 1;
				}
				size = 1 << pow;

				data_.resize(size);
			}
			w_ = r_ = 0;
		}
		int Write(vector<int> const &block)
		{
			int written_count = min(block.size(), data_.size() - (w_ - r_));
			for (int i = 0; i < written_count; ++i) {
				data_[w_++ % data_.size()] = block[i];
			}
			return written_count;
		}
		int Read(int count, vector<int> &out)
		{
			out.clear();
			int read_count = count > 0 ? min(count, w_ - r_) : 0;
			for (int i = 0; i < read_count; ++i) {
				out.push_back(data_[r_++ % data_.size()]);
			}
			return read_count;
		}

	private:
		int w_, r_;
		vector<int> data_;
};

- Alex September 03, 2017 | 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