## Amazon Interview Question for SDE-3s

• 0
of 0 votes

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
2
of 4 vote

So it's only 3 words that you need to look for? I was looking for a more generalized solution at first and thought it was pretty tricky. Here's how I would do it:

``````def minimum_distance_sum(words, a, b, c):
last_a = -1
last_b = -1
last_c = -1
min_distance = INT_MAX
for i, word in enumerate(words):
if word == a:
last_a = i
elif word == b:
last_b = i
elif word == c:
last_c = i

if last_a >= 0 and last_b >= 0 and last_c >= 0:
min_distance = min(min_distance, abs(last_a-last_c)+abs(last_a-last_b)+abs(last_b-last_c))

return min_distance``````

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

We note that the distance between the three words will always be equal to twice the distance between the two extreme words.

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ed2
{
public class Ed2
{
public static int MinDistance(IList<string> text, string word1, string word2, string word3)
{
int result = 0;

int position1 = -1;
int position2 = -1;
int position3 = -1;

for (int position = 0; position < text.Count(); position++)
{
if (text[position] == word1)
position1 = position;
else if (text[position] == word2)
position2 = position;
else if (text[position] == word3)
position3 = position;

if (position1 != -1 && position2 != -1 && position3 != -1)
{
int distance = (Max(position1, position2, position3) - Min(position1, position2, position3)) * 2;

if (result == 0 || distance < result)
result = distance;
}
}

return result;
}

static int Min(int a, int b, int c)
{
return Math.Min(a, Math.Min(b, c));
}

static int Max(int a, int b, int c)
{
return Math.Max(a, Math.Max(b, c));
}
}
}``````

``````using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ed2;

namespace Ed2UnitTest
{
[TestClass]
public class UnitTest
{
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(Ed2.Ed2.MinDistance(new string[] { "2", "1", "0", "2", "0", "3", "0" }, "1", "2", "3"), 8);
}

[TestMethod]
public void TestMethod2()
{
Assert.AreEqual(Ed2.Ed2.MinDistance(new string[] { "2", "1", "0", "2", "0", "3", "1" }, "1", "2", "3"), 6);
}
}
}``````

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

``````public int minDistance(String[] wrds, String[] arr)
{
if(wrds.length!=3 || arr==null||arr.length<3)
{
return -1;
}
int idxOne=-1;
int idxTwo=-1;
int idxThree=-1
HashMap<String,Boolean> w=new HashMap<String,Boolean>();
for(int i=0;i<wrds.length;i++)
{
w.put(wrds[i],i);
}

int min=-1;
for(int i=0;i<wrds.length;i++)
{
if(w.containsKey(wrds[i]))
{
int x=w.get(wrds[i]);
if(x==0)
{
idxOne=i;
}else if (x==1)
{
idxTwo=i;
}else{
idxThree=i;
}
}
if(idxOne!=-1 && idxTwo!=-1 && idxThree!=-1)
{
int diff=Math.max(idxOne,Math.max(idxTwo,IdxThree))-Math.min(idxOne,IdxTwo,IdxThree)
min=min==-1?diff:Math.min(min,diff);
}

}
return min;
}``````

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

``````- (int)minimumIndexDistance:(NSArray<NSString*> *)words
search:(NSArray<NSString*> *)searchWords {

int minIndexDistance = INT_MAX;
int index0 = -1, index1 = -1, index2 = -1;

NSSet *selectedWordsSet = [NSSet setWithArray:searchWords];
NSMutableDictionary<NSString*,NSNumber*> *selectionBucket = [NSMutableDictionary new];

for (int i = 0; i < words.count; i++) {
NSString *currentWord = words[i];

// check if count of stored digits is 3, then lets compute the differences of the indices
if (selectionBucket.count == 3) {
index0 = selectionBucket[searchWords].intValue;
index1 = selectionBucket[searchWords].intValue;
index2 = selectionBucket[searchWords].intValue;

int potentialMinIndex = abs(index0-index1) + abs(index0-index2) + abs(index1-index2);
minIndexDistance = MIN(potentialMinIndex, minIndexDistance);
}

if ([selectedWordsSet containsObject:currentWord]) {
selectionBucket[currentWord] = [NSNumber numberWithInt:i];
}
}

return minIndexDistance;
}``````

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