Interview Question for Software Engineer / Developers


Country: United States




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

int col,row, prevcol=0, prevrow=0;
for(int i=0; i<s.length(); i++)
{
	col  = ( s[i]-'a' ) % ROW_LEN;
	row = ( s[i]-'a' )   / ROW_LEN;

	while(col>prevcol) { putchar('r'); prevcol++; }
	while(col<prevcol) { putchar('l'); prevcol--; }

	while(row>prevrow) { putchar('d'); prevrow++; }
	while(row<prevrow) { putchar('u'); prevrow--; }
	
	putchar('!');
}

- S O U N D W A V E March 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

every time you need to update the precol and prevrow in the end of the for loop. (Sorry dismiss this message. I dismiss that you ++percol in the while loop)

- adiggo March 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

That's some super slick code.

- java is slow March 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I don't know why you posted it multiple times :) but it's a good solution. I wasn't able to code it in allotted time although I had similar approach. Thanks for sharing your code.

- msamarch1980 March 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a highschool question.

Scan the array to find next position (x,y).

Compare with previous one (xp, yp).

Keep going like that.

O(N^2 * stringlength).

- elite coder March 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The complexity is not right. between each successive spots there could be upto N up/down moves, and N left/right moves. So you might have to print 2N moves (u,d, right, left). So it will be O( n3 * str )

- Anonymous March 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class OnScreenKeyboard {
	public static void main(String[] a) {
		OnScreenKeyboard onScreenKeyboard = new OnScreenKeyboard();
		onScreenKeyboard.findLocation("dacixdsadsrascv", 5);
	}

	private void findLocation(String string, int length) {

		int currentRow = 1, currentCol = 1, row = 0, col = 0, ascii = 0;
		char array[] = string.toCharArray();
		StringBuilder sb = new StringBuilder();

		for (char element : array) {
			ascii = ((int) element) - 96;
			row = (ascii / length) + 1;
			col = ascii % length;

			for (int i = Math.abs(row - currentRow); i > 0; i--) {
				sb.append((currentRow < row) ? "d" : "u");
			}

			for (int i = Math.abs(col - currentCol); i > 0; i--) {
				sb.append((currentCol < col) ? "r" : "l");

			}
			sb.append("!");
			currentRow = row;
			currentCol = col;
		}
		System.out.print(sb.toString());
	}
}

- Ankit Garg March 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

for(int i=0; i<s.length(); i++)
{
	col  = ( s[i]-'a' ) % ROW_LEN;
	row = ( s[i]-'a' )   / ROW_LEN;

	while(col>prevcol) { putchar('r'); col--; }
	while(col<prevcol) { putchar('l'); col++; }

	while(row>prevrow) { putchar('d'); col--; }
	while(row<prevrow) { putchar('u'); col++; }
	
	putchar('!');
}

- S O U N D W A V E March 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

for(int i=0; i<s.length(); i++)
{
	col  = ( s[i]-'a' ) % ROW_LEN;
	row = ( s[i]-'a' )   / ROW_LEN;

	while(col>prevcol) { putchar('r'); col--; }
	while(col<prevcol) { putchar('l'); col++; }

	while(row>prevrow) { putchar('d'); row--; }
	while(row<prevrow) { putchar('u'); row++; }
	
	putchar('!');
}

- S O U N D W A V E March 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't upvote this. Let it die.

Where is the edit feature :(

- S O U N D W A V E March 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

Beside the date is the edit link.

- / March 20, 2014 | Flag


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