Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit AONECODE.COM for ONE-ON-ONE private lessons by FB, Google and Uber engineers!

SYSTEM DESIGN
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algorithms, Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Get hired from G, U, FB, Amazon, LinkedIn, Yahoo and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!

import java.util.Stack;
class CharNode {
    char ch;
    CharNode prev;
    CharNode next;

    public CharNode(char ch) { this.ch = ch; }
}

class Edit {
    String edition; //"DEL", "INS", "LEFT", "RIGHT"
    CharNode data;
    public Edit(String edition, CharNode cur) {
        data = cur;
        this.edition = edition;
    }
}

public class TextEditor {
    private Stack<Edit> undo_stack;
    private CharNode cursor;
    private CharNode end;

    public TextEditor() {
        undo_stack = new Stack();
        end = new CharNode('\0');
        cursor = end;
    }

    public void moveCursorLeft() {
        if(cursor.prev == null) return;
        cursor = cursor.prev;
        undo_stack.push(new Edit("RIGHT", null));
    }

    public void moveCursorRight() {
        if(cursor == end) return;
        cursor = cursor.next;
        undo_stack.push(new Edit("LEFT", null));
    }

    public void insertCharacter(char ch) {
        CharNode n = new CharNode(ch);
        insert(n);
        undo_stack.push(new Edit("DEL", null));
    }

    public void backspace() {
        if(cursor.prev == null) return;
        undo_stack.push(new Edit("INS", delete(cursor.prev)));
    }

    public void undo() {
        if(undo_stack.isEmpty()) return;
        Edit edit = undo_stack.pop();
        switch(edit.edition) {
            case "LEFT":
                cursor = cursor.prev; break;
            case "RIGHT":
                cursor = cursor.next; break;
            case "DEL":
                delete(cursor.prev); break;
            case "INS":
                insert(edit.data); break;
            default:
                return;
        }
    }

    public String toString() {
        StringBuilder text = new StringBuilder();
        CharNode n = end.prev;
        if(cursor == end) text.append('|');
        while(n != null) {
            text.insert(0, n.ch);
            if(cursor == n) text.insert(0, '|');
            n = n.prev;
        }
        return text.toString();
    }

    private void insert(CharNode node) {
        CharNode prev = cursor.prev;
        node.next = cursor;
        cursor.prev = node;
        if(prev != null) {
            prev.next = node;
            node.prev = prev;
        }
    }

    private CharNode delete(CharNode del) {
        if(del.prev != null) {
            del.prev.next = cursor;
        }
        cursor.prev = del.prev;
        return del;
    }
}

- aonecoding4 December 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Original Link: https://github.com/tabvn/ued/blob/master/careercup/google/design_text_editor.cpp

#include <iostream>
#include <vector>
#include <string>

const char CURSOR = '|';

struct Revision
{
	std::string text;
	int cursorIndex;

	Revision(){
		this->text = CURSOR;
		this->cursorIndex = 0;
	}

};


struct TextEditor
{

	std::vector<Revision> revisions; // store all revisions for undo
	int currentRevision;

	TextEditor(){

		this->currentRevision = 0;
		Revision r;

		this->revisions.push_back(r); 

		this->displayText("Start with empty text");
		
	}

	void displayText(std::string funcName){

		Revision revision = this->revisions[this->currentRevision];
		std::cout << funcName << std::endl;
		std::cout << "text = \"" << revision.text << "\"" << std::endl;
	}
	/*
	* Move cursor index to left
	*/
	void moveCursorLeft(){

		Revision r = this->revisions[this->currentRevision];
		char leftChar;

		if(r.cursorIndex > 0){
			leftChar = r.text[r.cursorIndex-1];
			r.text[r.cursorIndex-1] = CURSOR;
			r.text[r.cursorIndex] = leftChar;
			r.cursorIndex --;

			this->revisions.push_back(r);
			this->currentRevision ++;


		}

		this->displayText("moveCursorLeft()");




	}

	/*
	* Move cursor index to right
	*/
	void moveCursorRight(){

		Revision r = this->revisions[this->currentRevision];
		char rightChar;

		if(r.cursorIndex < r.text.size() -1){

			rightChar = r.text[r.cursorIndex+1];
			r.text[r.cursorIndex+1] = CURSOR;
			r.text[r.cursorIndex] = rightChar;
			r.cursorIndex ++;

			this->revisions.push_back(r);
			this->currentRevision ++;
		}

		this->displayText("moveCursorRight()");

	}

	/*
	* Insert the char right before cursor
	*/

	void insertCharacter(char ch){

		Revision r = this->revisions[this->currentRevision];
		r.text[r.cursorIndex] = ch;
		r.text += "|";
		r.cursorIndex++;

		this->revisions.push_back(r); // new revision become current revision
		this->currentRevision ++;

		std::string s = "insertCharacter('";
		s+= ch;
		s+= "')";
		this->displayText(s);
	}

	/*
	* delete the char right before cursor
	*/
	void backspace(){

		Revision r = this->revisions[this->currentRevision];
		if(r.text.size() > 1 && r.cursorIndex > 0){
				r.text.erase(r.cursorIndex - 1, 1);		
				r.cursorIndex --;

				this->revisions.push_back(r);
				this->currentRevision++;
		}

		this->displayText("backspace()");


	}

	/*
	* Undo last edit
	*/

	void undo(){

		if(this->currentRevision > 0){
			this->currentRevision--;
		}

		this->displayText("undo()");
	}
	
};



void beginTest(){

	TextEditor editor;

	editor.insertCharacter('a');
	editor.insertCharacter('b');
	editor.insertCharacter('c');

	editor.moveCursorLeft();
	editor.moveCursorLeft();
	editor.moveCursorLeft();

	editor.backspace();
	editor.moveCursorLeft();

	editor.undo();
	editor.undo();
	editor.undo();
	editor.undo();
	editor.undo();




}
int main(int argc, char const *argv[]){
	
	beginTest();

	return 0;
}

- toan@tabvn.com December 16, 2018 | 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