code editor diff between 2 people (Skype Interview)


  • 1
    J

    The problem:
    I'm working on a collaborative code editor between 2 people. I have the text
    synchronizing across both screens. However, I have run into the problem
    illustrated below.

    0_1476152938822_Screen Shot 2016-10-10 at 7.26.56 PM.png

    The content updates and the word cat shifts down a line. However, Person B's
    cursor is left in it's original position instead of moving down a line as expected.

    Implement the fixCursorRow method don't worry about updating the column.
    */

    public class Cursor {
        public int column;
        public int row;
    
        public Cursor(int row, int column);
    }
    
    // diff comes from the other screen.
    // content is content of the current screen before diff is applied.
    // cursor is the cursor of the current screen.
    
    // diff format:
    // The diff contains all content from beginning to end of document.
    
    {{"1", "\n"}, {"0", "cat"}}
    // house -> horse = {{"0", "ho"}, {"-1", "u"}, {"1", "r"}, {"0", "se"}}
    // "0" -> equality, "1" -> addition, "-1" -> removal
    public Cursor fixCursorRow(String[][] diff, String content, Cursor cursor) {
    }
    
    

  • 0
    A

    Were you able to solve this problem? I got the same problem but was not able to figure out
    the solution.


  • 0

    @aman35 Could you shed more light on the problem statement, please? Just explain it if it is possible in the way you understand it, because I think that I cannot understand the statement. Thank you very much


  • 0
    A

    HI @elmirap
    So my interviewer told me that two screens were given of Person A and Person B as shown in the figure and text synchronization between the screen has been already done. But they have one problem, they are not able to synchronize the cursor positions.
    So suppose in screen A cursor and cat are on line 1 and the cursor is behind the cat. So if Person A hits enter , both cursor and cat will move to line 2. Since the text is synchronized between the screens, the cat in the screen B is also being moved to the next line but the cursor in the same screen is still at line 1. So we have to synchronize the cursor position too.
    We have a following method that will give us the diff between the old and new content.
    public Cursor fixCursorRow(String[][] diff, String content, Cursor cursor) {
    }

    Note:
    1 means addition of something
    0 means nothing is changed
    -1 means removal of something

    So for this problem we get following diff array
    {{"1", "\n"}, {"0", "cat"}}

    So in the diff array you can see there is an addition of the new line {"1", "\n"} and also there is no change in the content {"0", "cat"}.This mean we have to move cursor to the next line.

    This was kind of very open ended question and I got only 25 min to give the solution.Please let me know if you want more explanation.


  • 0
    V

    I don't know if this is as simple as it sounds to me, but just iterate through the Diff pairs, and the position of the pair that has '\n' incremented by 2 will give the line on which the cursor should be, right?

    In the above example, \n is present in the 0th pair of the diff list. So, the cursor should be on the second line. (0 + 2 = 2).

    I doubt it is this simple, but who knows.


  • 0
    J

    I believe you should add 1 to the cursor passed in if it's a {"1", "\n"}, minus 1 if {"-1", "\n"}.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.