Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functions.
toBe(val) accepts another value and returns true if the two values === each other. If they are not equal, it should throw an error "Not Equal".notToBe(val) accepts another value and returns true if the two values !== each other. If they are equal, it should throw an error "Equal".Example 1:
Input: func = () => expect(5).toBe(5)
Output: {"value": true}
Explanation: 5 === 5 so this expression returns true.
Example 2:
Input: func = () => expect(5).toBe(null)
Output: {"error": "Not Equal"}
Explanation: 5 !== null so this expression throw the error "Not Equal".
Example 3:
Input: func = () => expect(5).notToBe(null)
Output: {"value": true}
Explanation: 5 !== null so this expression returns true.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem is like trying every single possible outcome to see if it meets the conditions. We go through each possibility step-by-step until we either find the right one or exhaust all the options. It's thorough but can be slow.
Here's how the algorithm would work step-by-step:
def find_best_combination_brute_force(items, condition_function, quality_function):
best_combination = None
best_quality = float('-inf')
for start_index in range(len(items)):
for end_index in range(start_index, len(items)):
# Create a combination from start_index to end_index
current_combination = items[start_index:end_index + 1]
# Check if the current combination meets the specified condition
if condition_function(current_combination):
# Calculate the quality of the current combination
current_quality = quality_function(current_combination)
# Determine if the current combination improves upon the best found so far
if current_quality > best_quality:
best_quality = current_quality
best_combination = current_combination
return best_combinationThe goal is to efficiently determine if we can transform one word into another by only changing one letter at a time, using a dictionary of valid words. We'll use a method that explores possible word transformations in a focused way, avoiding redundant paths.
Here's how the algorithm would work step-by-step:
def can_transform(begin_word, end_word, word_list):
word_set = set(word_list)
if end_word not in word_set:
return False
words_to_explore = [begin_word]
visited_words = {begin_word}
while words_to_explore:
current_word = words_to_explore.pop(0)
for i in range(len(current_word)):
for char_code in range(ord('a'), ord('z') + 1):
new_char = chr(char_code)
new_word = current_word[:i] + new_char + current_word[i+1:]
if new_word == end_word:
return True
# Only explore valid, unvisited words
if new_word in word_set and new_word not in visited_words:
words_to_explore.append(new_word)
#Mark as visited to avoid infinite loops
visited_words.add(new_word)
# If we exhaust all possibilities, there's no transformation
return False| Case | How to Handle |
|---|---|
| Input is a valid boolean True | The function correctly returns the string 'True'. |
| Input is a valid boolean False | The function correctly returns the string 'False'. |
| Input is None (Python) or null (Java/C++) | The function should raise a TypeError or NullPointerException, as the input is not a boolean. |
| Input is an integer 0 | The function should raise a TypeError, as the input is not a boolean (Python's truthiness rules do not apply here). |
| Input is an integer 1 | The function should raise a TypeError, as the input is not a boolean. |
| Input is a string 'True' or 'False' | The function should raise a TypeError, as the input is not a boolean. |
| Input is a different type that could be implicitly converted (e.g., in JavaScript) | The function should ensure strict type checking to only accept boolean values, preventing unexpected behavior due to implicit conversions. |
| Input is a boolean object (e.g., Boolean(true) in JavaScript) | The function correctly returns the string 'True' after automatic unboxing to primitive boolean true. |