## Amazon Interview Question for SDE-3s

• 0

Country: United States

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

I would hashset the array to remove duplicate items. Then find the min and max value. If (max-min) + 1 == hashset size then the array is contiguous, otherwise it is not.

``````public bool IsArrayContiguous(int[] arr)
{
if (arr == null || arr.Length == 0)
return false;

var dedupedItems = new HashSet<int>(arr);
int? minValue = null;
int? maxValue = null;

if (dedupedItems.Count == int.MaxValue) // array includes all possible integers
return true;

foreach (var val in dedupedItems)
{
if (minValue == null || minValue.Value > val)
minValue = val;

if (maxValue == null || maxValue.Value < val)
maxValue = val;
}

return (maxValue - minValue + 1 == dedupedItems.Count);
}``````

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

``````private static boolean evaluateConsecutive(int[] arr){
Set set = new TreeSet();

int a=-1,b=-1;
Iterator<Integer> it = set.iterator();
do {
a = (it.hasNext()) ? it.next() : -1;
b = (it.hasNext()) ? it.next() : -1;

if (b > 0 && b != a+1){
return false;
}
} while (it.hasNext());
return true;
}``````

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

``````def isContiguous(arr):
if len(arr)==0:
return False
mini=min(arr)
maxi=max(arr)
for i in range(mini,maxi+1):
if i not in arr:
return False
return True``````

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

``````def isContiguous(arr):
seen=set()
for i in range(len(arr)):
count=1
cur_el=arr[0]-1
while cur_el in seen:
count+=1
cur_el-=1
cur_el=arr[0]+1
while cur_el in seen:
count+=1
cur_el+=1
if count==len(seen):
return True
else:
return False``````

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.

### 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.