Amazon Interview Question for SDE-3s


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);
        }

- Dung Nguyen April 17, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static boolean evaluateConsecutive(int[] arr){
        Set set = new TreeSet();
        Arrays.stream(arr).boxed().forEach(x -> set.add(x));

        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;
    }

- A.A. April 06, 2019 | Flag Reply
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

- tuhinmaji314 October 24, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def isContiguous(arr):
	seen=set()
	for i in range(len(arr)):
		seen.add(arr[i])
	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

- tuhinmaji314 October 24, 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