Amazon Interview Question for SDE-2s


Country: United States
Interview Type: Phone Interview




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

use Boyer–Moore (BM) search algorithm.
O(n/m).
In loop in each iteration use BM twice:
1) find nearest "from:" entry,
2) find nearest "to:" entry. (Start from position found in search from step #1).
Collect a pair and go to next iteration, which starts at the position found in search #2 from previous iteration.

- zr.roman December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You could just use regex to do this:

Pattern pattern = Pattern.compile("(from:-?\\d+.\\d+,-?\\d+.\\d+)|(to:-?\\d+.\\d+,-?\\d+.\\d+)");
		Matcher matcher = pattern.matcher(string);
		while (matcher.find()) {
			if(matcher.group(1) != null) System.out.println(matcher.group(1));
			if(matcher.group(2) != null) System.out.println(matcher.group(2));
		}

- mariovalentim December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I suppose such regex commands are implemented as KMP or BM behind the scenes.

- zr.roman December 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Focus was to write a shell command/ script. I gave simple one:
$ IFS=$'\n'; for i in `cat log`; do from=`echo "$i" | grep -o 'from:[^ ]*'`; to=`echo "$i" | grep -o 'to:[^ ]*' $i`; echo "$from $to"; done

He said it might work but it doesn't any corrections here?

- AnonymousN December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My mistake... the second $i should not be there in grep for 'to:' part. It works.
Correct one:
$ IFS=$'\n'; for i in `cat log`; do from=`echo "$i" | grep -o 'from:[^ ]*'`; to=`echo "$i" | grep -o 'to:[^ ]*'`; echo "$from $to"; done

- AnonymousN December 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Better Format
$ IFS=$'\n';
for i in `cat log`;
do
from=`echo "$i" | grep -o 'from:[^ ]*'`;
to=`echo "$i" | grep -o 'to:[^ ]*'`;
echo "$from $to";
done

- asd December 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in Python 3.4:

log_in = """
some garbage...from:123.54,78.21...more garbage..to:56,82,124.54...more
some more garbage...from:11.54,45.84...garbage..to:115.87,98.65
"""

import re
regex = re.compile("from:([\d\.\,]+\d).*?to:([\d\.\,]+\d)")
coordinate_list = regex.findall(log_in)

- geoff January 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

grep -o "\(from\|to\):[0-9]*[.][0-9]*[,][0-9]*[.][0-9]*" log_file.txt | xargs -n2 | awk '{ print "(" $1 ", " $2 ")" }' | sed -e 's/from://' -e 's/to://'

Result:

(123.54,78.21, 56.82,124.54)
(11.54,45.84, 115.87,98.65)

- eugene.pavlyuk June 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

perl -wnl -e '
 m#from:\K[0-9]+(\.[0-9]+)?,[0-9]+(\.[0-9]+)?# and $from=$&;
 m#to:\K[0-9]+(\.[0-9]+)?,[0-9]+(\.[0-9]+)?#   and $to=$&;
 print "( $from , $to )" ' test

overhere test is the name of the log file
\K is a special regex meta character that matches but does not output its preceding group

Output:
( 123.54,78.21 , 56.82,124.54 )
( 11.54,45.84 , 115.87,98.65 )

- Clyton Dantis July 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

grep -o "FROM:[0-9]\{2,3\}.[0-9]\{2,3\},[0-9]\{2,3\}.[0-9]\{2,3\}\|TO:[0-9]\{2,3\}.[0-9]\{2,3\},[0-9]\{2,3\}.[0-9]\{2,3\}" m1.txt | xargs -n 2

should work exception being if FROM or TO spans multiple lines.

- Me May 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{SOLVED}:
echo "Given a log file:some garbage...from:123.54,78.21...more garbage..to:56,82,124.54...more some more garbage...from:11.54,45.84...garbage..to:115.87,98.65 ">file.txt

cat file.txt | grep -o -e 'from:' -e '[1-9]' -e 'to:'

- Mohit Gupta May 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

cat filename | grep -o -e 'from:' -e '[1-9]' -e 'to:'

- Mohit Gupta May 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

grep -Eo '[0-9]{1,3}[.,][0-9]{1,2}[.,][0-9]{1,3}[.,][0-9]{1,2}' file.txt | sed -e 'N; s/\n/ /' | awk '{ print "{"$1","$2"}"}'

- Deepika February 23, 2019 | 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