## 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, 'result', find_min_diff(d), 'expected', d)``````

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

Indiatweets.im

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, 'result', find_min_diff(d), 'expected', d)

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.

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.