HRT (Hudson River Trading) | OA | Mini vi Editor

Anonymous User
1293
Dec 16, 2023
Dec 16, 2023

minivi is a basic text editor with a few advanced features. Unlike most text editors, minivi can only edit one line.

The text buffer starts empty. Your solution should accept a string of keystrokes and returns the final contents of the text buffer.

This text editor has two primary modes, command mode and insert mode. In command mode, keystrokes trigger some action. In insert mode, keystrokes are written to the text buffer except ~ and < which are special insert mode commands. In command mode, the last valid position of the cursor is on the last character of the text buffer or at position 0 if the text buffer is empty. In insert mode, the cursor is allowed to be one position past the last character in the text buffer. When a character is written, it is written at the location of the cursor, and the cursor as well as any character at or after the cursor are shifted forward by one.

Commands:
"~" Only in insert mode! exit insert mode.
"<" Only in insert mode! Delete the character before the cursor.
"h" Move the cursor one character backward.
"l" Move the cursor one character forward.
"b" Move the cursor to start of the first word starting before the cursor, or the first character if there is no suitable start-of-word boundary.
"w" Move the cursor to start of the following word or the last character if there is no suitable start-of-word boundary.
"i" Enter insert mode
"a" Enter insert mode and move the cursor one character forward.

For the b and w commands, a start-of-word boundary is any non-space character preceded by a space. The standard space character is the only whitespace character.

Examples:

Test case examples with the final cursor position in bold text. (the final cursor position is not part of solution's result)

Input: ihello world~
exiting insert mode moves the cursor back to the last position within the text buffer
Output: hello world

Input: iaaa~ ibbb~
entering insert mode again, the cursor is on the a so it gets pushed further as the cursor inserts characters
Output: aabbba

Input: iaaa~ abbb~
the "a" command shifts the cursor after entering insert mode which allows us to append to the line
Output: aaabbb

Input: i012345<<<~
Output: 012

Input: ithe quick brown fox~ b
Output: the quick brown fox

Input: ithe quick brown fox~ bb
Output: the quick brown fox

Input: ithe quick brown fox~ bbbbbbbbb
Output: the quick brown fox

Input: ithe quick brown fox~ bbbbbbbbb w
Output: the quick brown fox

Input: ithe quick brown fox~ bbbbbbbbb ww
Output: the quick brown fox

No constraints were given but all operations can have O(1) time complexity. Many edge cases to be considered. Implemented doubly linked list solution, still missed an edge case perhaps and got 18/20 test cases passed.

Comments (5)