This is a design round. You write the classes, and the interviewer checks that they behave.
Here you build Editor, a tiny text editor. Its edits can be undone and redone. The cursor always stays at the end of the text.
Implement the class:
Editor()starts with an empty text and nothing to undo or redo.write(word)addswordat the end of the text.erase(count)removes the lastcountcharacters. It returns how many it removed.undo()reverses the newest edit that isn't undone yet. It returnstrueif there was one.redo()does the newest undone edit again. It returnstrueif there was one.length()returns how many characters the text has.tail(n)returns the lastncharacters of the text.
An edit is a call to write, or a call to erase that removes something.
When the text has fewer than count characters, erase removes all of them. On an empty text it removes nothing and returns 0. That call is not an edit, and nothing changes.
Undoing a write takes its word off the end again. Undoing an erase puts the removed characters back at the end. Redo then does that same edit again.
Each undo goes one more edit back in time. Each redo does the edit that was undone last. With nothing to undo or redo, the call returns false and changes nothing.
A new edit ends the redo history. Right after it, redo returns false.
When the text has fewer than n characters, tail returns the whole text.
The calls run in order on one new Editor. A call to write writes nothing. A call to erase or length writes the number. A call to undo or redo writes True or False. A call to tail writes the text. For an empty text, it writes (empty).
Example 1:
Input:
Editor()
1. write("cat")
2. write("dog")
3. erase(2)
4. tail(10)
5. undo()
6. tail(10)
7. undo()
8. redo()
9. length()
Output:
2
catd
True
catdog
True
True
6
Explanation:
After calls 1 and 2, the text is "catdog".
Call 3 erases "og", so the text is "catd".
Call 5 undoes the erase. The text is "catdog" again.
Call 7 undoes the write of "dog". The text is "cat".
Call 8 redoes that write.
The text is "catdog" again, with 6 characters.
Example 2:
Input:
Editor()
1. write("ab")
2. undo()
3. erase(4)
4. redo()
5. erase(5)
6. tail(3)
7. undo()
8. undo()
9. undo()
Output:
True
0
True
2
(empty)
True
True
False
Explanation:
Call 2 undoes the write of "ab". The text is empty.
Call 3 removes nothing and returns 0. It is not an edit.
So call 4 can still redo the write. The text is "ab" again.
Call 5 asks for 5 characters, but only 2 are there.
Calls 7 and 8 undo the erase and the write.
Call 9 finds nothing left to undo.
Example 3:
Input:
Editor()
1. write("one")
2. write("two")
3. undo()
4. write("six")
5. redo()
6. undo()
7. redo()
8. tail(4)
Output:
True
False
True
True
esix
Explanation:
Call 3 undoes the write of "two". The text is "one".
Call 4 is a new edit, so "two" can't come back.
Call 5 finds nothing to redo.
Call 6 undoes the write of "six", and call 7 redoes it.
The text is "onesix". Its last 4 characters are "esix".
Constraints:
wordhas 1 to 20 lowercase English letters.countis a whole number from 1 to 100.nis a whole number from 1 to 20.- At most 100,000 calls follow the constructor.
Note:
Each call should take O(k) time, where k is the size of its own edit, at most 100 characters. The length of the text should not matter. Saving a copy of the whole text on every edit would be too slow. That takes O(n) time per edit, for a text of n characters.
Free plan: 30 tutor messages. PRO is unlimited.
