Interview Question for Software Developers


Country: United States




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

The code below should work.

The logic is pretty simple, imagine the keyboard as a matrix. If the keyboard size is 26, it is a matrix with 1 row, 26 columns. When the keyboard size is 13, it is a matrix with 2 row and 13 columns.

All we have to now do is compute the moves from source to destination for each character.

For "Hi"
0,0 -> 0,8 (move by 8 columns -> R8)
0,8 -> 0,9 (move by 1 column -> R1)

def robo_hand_moves(input,keyboard_size)
  char_map = {"A"=>1, "B"=>2, "C"=>3, "D"=>4, "E"=>5, "F"=>6, "G"=>7, "H"=>8, "I"=>9, "J"=>10, "K"=>11, "L"=>12, "M"=>13, "N"=>14, "O"=>15, "P"=>16, "Q"=>17, "R"=>18, "S"=>19, "T"=>20, "U"=>21, "V"=>22, "W"=>23, "X"=>24, "Y"=>25, "Z"=>26}
  len = input.size
  source_x=0
  source_y=0
  robo_moves = []
  for i in (0...input.size)
    char_order = char_map[input[i]]
    dest_x = (char_order/keyboard_size).to_i
    dest_y = (char_order%keyboard_size)
    next_move = robo_moves(source_x,source_y,dest_x,dest_y)
    robo_moves+= next_move unless next_move.empty?
    robo_moves<< "T"
    source_x=dest_x
    source_y=dest_y
  end
  puts robo_moves.join(",")
end

def robo_moves(x1,y1,x2,y2)
  result = []
  row_wise_dist = (x2-x1)
  if(row_wise_dist<0)
    result<<"U"+row_wise_dist.abs.to_s
  elsif(row_wise_dist>0)
    result<<"D"+row_wise_dist.abs.to_s
  end
  col_wise_dist = (y2-y1)
  if(col_wise_dist<0)
    result<<"L"+col_wise_dist.abs.to_s
  elsif(col_wise_dist>0)
    result<<"R"+col_wise_dist.abs.to_s
  end
  result
end


begin
  line = ARGV[0]
  input = line.split(",")
  robo_hand_moves(input[0],input[1].to_i)
end

- kirubakaran.d November 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

/**
* Java Solution
* O(n) time complexity and O(1) space complexity
*/
public static void process(String input, int width) {
    int currentRow = 0;
    int currentCol = -1;

    for (int i = 0; i < input.length(); i++) {
      int next = input.charAt(i) - 'A'; //next character

      /** convert next character in to 2D location (row and col) based on input width of keyboard */
      int row = next / width;
      int col = next % width;

      /** move robot hand from currentRow, currentCol to row, col */
      if (row < currentRow) {
        System.out.print("U" + (currentRow - row) + ", ");
      } else if (row > currentRow){
        System.out.print("D" + (row - currentRow) + ", ");
      }

      if (col < currentCol) {
        System.out.print("L" + (currentCol - col) + ", ");
      } else if (col > currentCol){
        System.out.print("R" + (col - currentCol) + ", ");
      }

      if (i < input.length() - 1) {
        System.out.print("T, ");
      } else {
        System.out.print('T');
      }

      /** change currentRow and currentCol to new location */
      currentRow = row;
      currentCol = col;
    }
  }

- Sabbir Manandhar November 08, 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