Given an array of integers, you need to solve two problems:
- Find the maximum difference between two elements such that the larger element appears after the smaller element.
- Find the minimum number of steps required to sort the array in non-decreasing order where a step is defined as incrementing any element by 1.
Implement these two functionalities in Python.
Please sign-in to view the solution
Given a 2D array (matrix) of integers, write a function that applies the following transformation:
Flip the matrix horizontally (i.e., reverse the order of elements in each row).
Implement this transformation in Python and apply it to the given matrix.
Example:
Input:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Output:
[
[3, 2, 1],
[6, 5, 4],
[9, 8, 7]
]
Please sign-in to view the solution
Given a binary tree, write a function to invert the binary tree (i.e., swap the left and right children of every node).
Implement this transformation in Python.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
4
/ \
7 2
/ \ / \
9 6 3 1
Please sign-in to view the solution
Given an array of integers, an equilibrium point is an index such that the sum of elements at lower indices is equal to the sum of elements at higher indices. Write a function in Python to find the first equilibrium point in the array. If no such point exists, return -1.
Example:
Input:
[1, 3, 5, 2, 2]
Output:
2 (since 1 + 3 = 2 + 2)
Please sign-in to view the solution
Given an array of integers, implement a function in Python that returns a new array where each element at index i
is the product of all the elements in the original array except the one at i
. You are not allowed to use division. Optimize for O(n) time complexity. Provide test cases to validate your solution.
Please sign-in to view the solution
Given an array of integers where every element appears twice except for one, write a function to find that single element. Explain the approach and also provide a code solution in Python.
Please sign-in to view the solution
Given two unsorted arrays of integers, write a function to determine if the arrays contain the same elements, regardless of their order. Explain your approach and provide a Python code solution.
Please sign-in to view the solution
Write a Python function to print all prime numbers up to a given limit. Explain your approach briefly.
Please sign-in to view the solution
Write a Python function to sort three numbers. Explain your approach briefly.
Please sign-in to view the solution
Given an array of strings, write a function to encode the entire array into a single string, and another function to decode this single string back into the original array of strings.
Requirements:
- The encoded string should be as compact as possible.
- Ensure no ambiguity in delimiting the individual strings.
Example:
array = ["apple", "banana", "cherry"]
encoded_str = encode(array)
decoded_array = decode(encoded_str)
assert decoded_array == array
Please sign-in to view the solution
Given an array, write a function to generate all possible permutations of the array elements.
Requirements:
- The function should return a list of all possible permutations.
- Each permutation should be in the form of a list.
Example:
array = [1, 2, 3]
permutations = generate_permutations(array)
assert permutations == [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
Please sign-in to view the solution
Given two integer arrays, write a function to find the common numbers as well as the different numbers in both arrays. Return two lists: one with the common numbers and one with the different numbers.
Requirements:
- The common numbers should appear only once in the result, even if they appear multiple times in the input arrays.
- The different numbers should include all numbers that are present in one array but not the other.
Example:
array1 = [1, 2, 3, 4, 4]
array2 = [3, 4, 5, 6]
common, different = find_common_and_different(array1, array2)
assert common == [3, 4]
assert different == [1, 2, 5, 6]
Please sign-in to view the solution
Write a function in Python that performs two tasks:
- Given a linked list, convert it into a 2D array where each row contains two consecutive linked list values.
- Given a 2D array, convert it back into a linked list where each element points to the next.
Requirements:
- Assume the linked list has an even number of elements.
- The 2D array should have a shape [n/2, 2], where n is the length of the linked list.
Example:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# Linked list: 1 -> 2 -> 3 -> 4
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4))))
# Task 1
array = linked_list_to_2d_array(head)
assert array == [[1, 2], [3, 4]]
# Task 2
new_head = array_to_linked_list(array)
# Validate new_head to be a linked list: 1 -> 2 -> 3 -> 4
assert new_head.val == 1
assert new_head.next.val == 2
assert new_head.next.next.val == 3
assert new_head.next.next.next.val == 4
Please sign-in to view the solution
Given a linked list and an integer value k, write a function to return the kth element from the end of the linked list.
Requirements:
- You can assume k is a valid integer that will not exceed the length of the linked list.
- The function should handle linked lists efficiently.
Example:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# Linked list: 1 -> 2 -> 3 -> 4 -> 5
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))
k = 2
result = find_kth_from_end(head, k)
assert result == 4 # The 2nd element from the end is 4
Please sign-in to view the solution
Given millions of rows of numerical data, write a method to efficiently find the median value. The data cannot be loaded into memory all at once due to its size.
Requirements:
- The method should handle data that does not fit into memory.
- The solution should be efficient in terms of time and space complexity.
Example:
# Let's assume we have a method `read_streaming_data` that yields data row by row
def find_median_large_dataset(streaming_data):
# Implementation here...
# Example usage
streaming_data = read_streaming_data('large_dataset.csv')
median = find_median_large_dataset(streaming_data)
print(median)
Please sign-in to view the solution
Given an array of integers, write a function to find the maximum and the second largest maximum values in the array.
Requirements:
- The function should have a runtime complexity of O(n).
- Describe how you would test this function.
Example:
array = [3, 2, 1, 5, 4]
max_val, second_max_val = find_max_and_second_max(array)
assert max_val == 5
assert second_max_val == 4
Please sign-in to view the solution
Given a sorted array of integers, write a function to remove any duplicates. Describe your algorithm, provide the code implementation, and list how you would test your program.
Please sign-in to view the solution
Write a program in Python to find the depth of a binary search tree without using recursion.
Please sign-in to view the solution
Explain the time complexity of operations in a Binary Search Tree (BST). Provide both average-case and worst-case complexities.
Please sign-in to view the solution
Provide counter examples to the time complexity analysis of a Binary Search Tree (BST).
Please sign-in to view the solution
Write a program that takes a list of integers and a list of pairs of indices as input. The program should swap the elements at the given indices for each pair. Provide an example to illustrate the functionality.
Please sign-in to view the solution
Write a program that takes a 2D matrix as input and outputs the elements in spiral order. Provide an example to illustrate the functionality.
Please sign-in to view the solution
Explain and provide examples of software testing and development coding questions that involve basic data structures. Consider questions that cover arrays, linked lists, stacks, and queues. Provide a detailed solution for each example.
Please sign-in to view the solution
Given a string, remove the consecutively repeated characters. For example - aabbbcabcbb
to cabc
.
Please sign-in to view the solution
Given an array, locate the three greatest numbers and print their product.
Please sign-in to view the solution
Write a code to convert a string such as aaabbccdaa
to a3b2c2d1a2
.
Please sign-in to view the solution
Compress a string. For example, aaabbbcccccddddd
should be converted to a3b3c4d4
.
Please sign-in to view the solution
Find the number of occurrences of each character in a given string.
Please sign-in to view the solution
Given an array of strings, find the common prefix among them.
Please sign-in to view the solution
Write a program to group anagrams from a given list of strings. This is a common problem on LeetCode.
Please sign-in to view the solution
Given a sentence, write a function in Python to find the longest palindromic substring within it. A palindrome is a string that reads the same backward as forward.
Please sign-in to view the solution
Write a function in Python to find the sum of all digits in a given number.
Please sign-in to view the solution
Write a function in Python to traverse an array both from back to front and vice versa. Print the elements in both orders.
Please sign-in to view the solution
- Given a number, find the nearest palindrome.
- Given an array of length
n
containing numbers between 1 to 99, print the last two digits of the result after multiplying alln
numbers in the array.
Please sign-in to view the solution
As a software engineer, you are tasked with optimizing a given piece of code to improve its efficiency in terms of Big O notation. Describe at least five general strategies you would employ to enhance the code's performance.
Please sign-in to view the solution
Write a function in Python to check if the binary representation of a given number is a palindrome. A palindrome is a sequence that reads the same backward as forward.
Please sign-in to view the solution
- How would you test the search form on Amazon?
- Write a simple program in Java to get the second greatest number in an array.
Please sign-in to view the solution
Write a function in Python to find a pair of numbers in an array such that their product is the maximum possible.
Please sign-in to view the solution
Write a function in Python to check if the first string is an anagram of the second string. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Please sign-in to view the solution
Write a function in any language that will print "AN" if a number is divisible by 8, "ANIM" if divisible by 16, and "ANIMAL" if divisible by 32.
Please sign-in to view the solution
- Write a function to find the closest palindrome number to a given number.
- Write a function to determine if an array contains duplicate numbers.
- Write a function to print numbers in a rhombus shape:
1 121 12321 121 1
Please sign-in to view the solution
Given an array with numbers from 1 to 5, write a function in Python to subtract one number from each index.
Please sign-in to view the solution
- Write test cases for a video calling app on a mobile device.
- Write a program to find consecutive duplicate letters in each word of the sentence and print the sentence without duplicate letters.
- Find a series of numbers in an array which gives the sum value as 3.
Please sign-in to view the solution
What is binary search?
Please sign-in to view the solution
Given a string sentence, write a function to reverse the order of words without using any library functions for reversing strings or splitting words. For example, if the input string is 'The quick brown fox', the output should be 'fox brown quick The'.
Please sign-in to view the solution
Given a list of integers, write a function to group all positive numbers, zeros, and negative numbers together without using any additional data structures like sets or collections. Maintain the original relative order of elements within the same group. For example, if the input list is [4, -1, 0, 3, -2, 0, 1]
, the output should be [4, 3, 1, 0, 0, -1, -2]
.
Please sign-in to view the solution
Given an unsorted list of integers, write a function to find the second largest value in the list. You are not allowed to sort the list or use any built-in functions that directly identify the largest or second largest values. For example, if the input list is [3, 1, 4, 1, 5, 9, 2, 6]
, the output should be 6
.
Please sign-in to view the solution
Given a list of integers ranging from 1 to 100, write a function to identify and return the prime numbers from the list. For example, if the input list is [10, 15, 23, 50, 7]
, the output should be [23, 7]
.
Please sign-in to view the solution
How can you swap two INTEGER variables A and B without using a third temporary variable in C++? Provide the code and explain how it works.
Please sign-in to view the solution
As a software engineer at Oracle, demonstrate your understanding of logical programming and algorithms. Provide examples in Java for the following:
- A program to check if a string is a palindrome.
- A sorting algorithm (such as Bubble Sort) with an explanation.
- Basics of Java language concepts, such as loops and conditionals, with a simple example.
Please sign-in to view the solution
Write a program in Python to reverse a given string. The program should take a string as input and return the reversed string as output.
Please sign-in to view the solution
Describe the method to check if there is a loop in a linked list. Provide the algorithm and example code in Python.
Please sign-in to view the solution
Write a Java program to check if a given string is a palindrome. A palindrome is a word, number, phrase, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).
Please sign-in to view the solution