Bloomberg Interview Question: Design a Double-Ended Queue with O(1) Operations

10 Views
No Comments
Implement a queue which has "addfront, addback, popfront, popback, getSize" in O(1)

This Bloomberg question asks you to design a deque-like data structure that supports inserting and removing from both ends, plus querying its size, all in constant time. The natural approach is to use a doubly linked list (or dynamic circular array) and maintain head, tail, and size fields so each operation touches only a few pointers.

Bloomberg Interview Question: Minimum Letter Changes to Make Two Words Anagrams

Find out how close two words are to being anagrams of each other. A string s1 is an anagram of another string s2 if the same characters exist in both s1 and s2 in any order.

Write a function which accepts two strings, and returns the minimum number of letters that must be changed to make a word an anagram of another?

For example, to make 'bond' an anagram of 'down' you need to change 1 letter: 'b' to 'w'.

If either string contains a number or the strings are different lengths, throw an exception.

This problem defines“closeness”as the minimum number of character substitutions needed to turn one word into an anagram of the other. You should first validate inputs (same length, alphabetic only), then count character frequencies and compute how many positions differ, typically summing the positive differences and dividing appropriately.

Bloomberg Interview Question: Can a Car Reach the Oasis Before Running Out of Gas?

# Implement a ride(desert, gas) method that returns true if a car can reach an oasis
# before it runs out of gas and false otherwise. The car can drive in four directions:
# top, bottom, left, right. Moving one field requires one unit of gas (used gas is an
# equivalent of the number of steps taken).

# The desert is a 2D m x n array with five types of fields:

# 'c' - the starting point of the car
# 'o' - the oasis, our destination
# '.' - sand, the car can drive through it

desert = [
    ['.', '.', '.', '.', '.', '.', '.', 'o'],
    ['.', '.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.', 'c'],
]

# ride(desert, 3) => false
# ride(desert, 5) => true

Here you must decide whether a car, starting from cell ‘c’ on a grid, can reach cell ‘o’ using at most gas steps, moving in four directions through sand cells ‘.’. The natural solution is a shortest-path search such as BFS from the start; if the minimum distance to the oasis is ≤ gas, return true, otherwise false.

Bloomberg Interview Question: Substrings in the Most Deeply Nested Brackets

Given a string that may contain brackets, and no unbalanced brackets,
find the substring(s) within the most deeply nested balanced bracket(s).
The following sets of characters should be considered as open/close brackets respectively: (), [], { }
If there are multiple sets of brackets with the same highest depth, your function should return all substrings.
If there are no brackets in the string, then your function should return the entire input string.

"ab(c(d)e)"        -> "d"
"[a{b}c]d(e)]"     -> "b"
"((a)b(cd)ef)"     -> "a", "cd"
"(ab[]c){d{e}}"    -> "","e""Hello, World!"    -> "Hello, World!"

This problem asks you to scan a string, track nesting depth of (), [], and {} brackets, and collect all substrings that lie inside brackets at the maximum depth. A typical approach is a single pass with a stack or depth counter, recording indices when depth increases and updating the current maximum depth while extracting the content ranges.

END
 0