Bloomberg LP Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

def find_min_diff(arr):
    arr = sorted(arr)
    min_diff = None
    for i in range(1, len(arr)):
        diff = arr[i]-arr[i-1]
        if min_diff is None or min_diff>diff:
            min_diff = diff
    return min_diff


if __name__=='__main__':
    data = [
            [[100, 20, 52, 18, -4], 2]
           ]

    print('Find minimum difference between items in array')
    for d in data:
        print('array is', d[0], 'result', find_min_diff(d[0]), 'expected', d[1])

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

Indiatweets.im

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

def find_min_diff(arr):
arr = sorted(arr)
min_diff = None
for i in range(1, len(arr)):
diff = arr[i]-arr[i-1]
if min_diff is None or min_diff>diff:
min_diff = diff
return min_diff


if __name__=='__main__':
data = [
[[100, 20, 52, 18, -4], 2]
]

print('Find minimum difference between items in array')
for d in data:
print('array is', d[0], 'result', find_min_diff(d[0]), 'expected', d[1])

- Anonymous January 16, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the question says that the difference has to be absolute, then sorting the array and finding the adjacent difference is the most appropriate solution. But if the question says finding the smallest difference out of all the pairs from an array i.e min_diff can be less than 0, then we can find it with the following code.

MIN_DIFF(ARRAY){
		MAX_ELEMENT := FIND_MAX(ARRAY) //O(n)
		MIN_ELEMENT := FIND_MIN(ARRAY) //O(n)
		return MIN_ELEMENT - MAX_ELEMENT
	}

This this runs in O(n) time and O(1) space.

- Hardik Chauhan February 22, 2020 | 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