profile pic Algorithms
Upvote 0 Downvote
Array Operations: Finding Maximum and Minimum Quality Assurance @ Google Difficulty medium

Given an array of integers, you need to solve two problems:

  1. Find the maximum difference between two elements such that the larger element appears after the smaller element.
  2. 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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
2D Array Transformation Quality Assurance @ Google Difficulty medium

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]
]
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Binary Tree Structure Modification Quality Assurance @ Google Difficulty hard

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
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Finding Equilibrium Point in an Array Quality Assurance @ Google Difficulty medium

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)
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Product of All Elements Except for Index Quality Assurance @ Google Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find Non-Duplicate Element in an Array Quality Assurance @ Google Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Compare Elements of Two Unsorted Arrays Quality Assurance @ Google Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Print Prime Numbers up to a Given Limit Quality Assurance @ Google Difficulty easy

Write a Python function to print all prime numbers up to a given limit. Explain your approach briefly.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Sort Three Numbers Quality Assurance @ Google Difficulty easy

Write a Python function to sort three numbers. Explain your approach briefly.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Encode and Decode an Array of Strings Quality Assurance @ Google Difficulty medium

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
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Generate All Permutations of an Array Quality Assurance @ Google Difficulty medium

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]]
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find Common and Different Numbers in Two Arrays Quality Assurance @ Google Difficulty medium

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]
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Linked Lists and Multi-Dimensional Arrays Quality Assurance @ Google Difficulty hard

Write a function in Python that performs two tasks:

  1. Given a linked list, convert it into a 2D array where each row contains two consecutive linked list values.
  2. 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
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find Kth Element from the End in a Linked List Quality Assurance @ Google Difficulty medium

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
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find the Median from a Large Dataset Quality Assurance @ Google Difficulty hard

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)
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find the Maximum and Second Maximum in an Array Quality Assurance @ Google Difficulty medium

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
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Remove Duplicates from a Sorted Array Quality Assurance @ Google Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find Depth of Binary Search Tree Without Recursion Quality Assurance @ Google Difficulty medium

Write a program in Python to find the depth of a binary search tree without using recursion.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Time Complexity of Binary Search Tree (BST) Quality Assurance @ Google Difficulty easy

Explain the time complexity of operations in a Binary Search Tree (BST). Provide both average-case and worst-case complexities.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Counter Examples to the Time Complexity of Binary Search Tree (BST) Quality Assurance @ Google Difficulty medium

Provide counter examples to the time complexity analysis of a Binary Search Tree (BST).

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Swapping Integers in a List Quality Assurance @ Amazon Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Listing a Matrix in Spiral Order Quality Assurance @ Amazon Difficulty medium

Write a program that takes a 2D matrix as input and outputs the elements in spiral order. Provide an example to illustrate the functionality.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Software Testing and Development: Coding Questions Involving Basic Data Structures Quality Assurance @ Amazon Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Remove Consecutive Repeated Characters from String Quality Assurance @ Amazon Difficulty medium

Given a string, remove the consecutively repeated characters. For example - aabbbcabcbb to cabc.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find the Product of the Three Greatest Numbers in an Array Quality Assurance @ Amazon Difficulty medium

Given an array, locate the three greatest numbers and print their product.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Convert String to Run-Length Encoding Quality Assurance @ Amazon Difficulty easy

Write a code to convert a string such as aaabbccdaa to a3b2c2d1a2.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Compress a String Using Run-Length Encoding Quality Assurance @ Amazon Difficulty easy

Compress a string. For example, aaabbbcccccddddd should be converted to a3b3c4d4.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find the Number of Occurrences of Each Character in a String Quality Assurance @ Amazon Difficulty easy

Find the number of occurrences of each character in a given string.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Find the Common Prefix in an Array of Strings Quality Assurance @ Amazon Difficulty medium

Given an array of strings, find the common prefix among them.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Group Anagrams - LeetCode Problem Quality Assurance @ Amazon Difficulty medium

Write a program to group anagrams from a given list of strings. This is a common problem on LeetCode.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Finding the Longest Palindrome in a Sentence Quality Assurance @ Amazon Difficulty hard

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Finding the Sum of Digits in a Number Quality Assurance @ Amazon Difficulty easy

Write a function in Python to find the sum of all digits in a given number.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Traversing an Array from Back to Front and Vice Versa Quality Assurance @ Amazon Difficulty easy

Write a function in Python to traverse an array both from back to front and vice versa. Print the elements in both orders.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Coding Interview - Nearest Palindrome and Last Two Digits of Product Quality Assurance @ Amazon Difficulty hard
  1. Given a number, find the nearest palindrome.
  2. Given an array of length n containing numbers between 1 to 99, print the last two digits of the result after multiplying all n numbers in the array.
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Suggestions for Improving Code Efficiency (Big O) Quality Assurance @ Amazon Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Checking if a Binary Representation of a Number is a Palindrome Quality Assurance @ Amazon Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Testing the Search Form on Amazon and Coding for 2nd Greatest Number in an Array Quality Assurance @ Amazon Difficulty medium
  1. How would you test the search form on Amazon?
  2. Write a simple program in Java to get the second greatest number in an array.
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Finding a Pair in an Array with the Maximum Product Quality Assurance @ Amazon Difficulty medium

Write a function in Python to find a pair of numbers in an array such that their product is the maximum possible.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Check if One String is an Anagram of Another Quality Assurance @ Amazon Difficulty easy

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Function to Print Based on Divisibility Quality Assurance @ Amazon Difficulty easy

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Various Examples: Closest Palindrome, Array Duplicates, and Rhombus Shape Quality Assurance @ Amazon Difficulty medium
  1. Write a function to find the closest palindrome number to a given number.
  2. Write a function to determine if an array contains duplicate numbers.
  3. Write a function to print numbers in a rhombus shape:
    1
    121
    12321
    121
    1
    
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Subtracting One Number from Each Index in an Array Quality Assurance @ Amazon Difficulty easy

Given an array with numbers from 1 to 5, write a function in Python to subtract one number from each index.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Test Case Enumeration, Removing Consecutive Duplicates, and Finding Sum Series Quality Assurance @ Amazon Difficulty medium
  1. Write test cases for a video calling app on a mobile device.
  2. Write a program to find consecutive duplicate letters in each word of the sentence and print the sentence without duplicate letters.
  3. Find a series of numbers in an array which gives the sum value as 3.
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
What is Binary Search? Quality Assurance @ Amazon Difficulty easy

What is binary search?

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Reversing Words in a Sentence Quality Assurance @ Amazon Difficulty medium

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'.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Grouping Positive, Zero, and Negative Numbers Quality Assurance @ Amazon Difficulty medium

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].

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Finding the Second Largest Value in an Unsorted List Quality Assurance @ Amazon Difficulty medium

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Finding Prime Numbers in a List Quality Assurance @ Amazon Difficulty medium

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].

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Swapping Two Integer Variables Without a Third Variable Quality Assurance @ Oracle Difficulty easy

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Logical Programming and Algorithm Questions in Java Quality Assurance @ Oracle Difficulty medium

As a software engineer at Oracle, demonstrate your understanding of logical programming and algorithms. Provide examples in Java for the following:

  1. A program to check if a string is a palindrome.
  2. A sorting algorithm (such as Bubble Sort) with an explanation.
  3. Basics of Java language concepts, such as loops and conditionals, with a simple example.
Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Write a Program to Reverse a String Quality Assurance @ IBM Difficulty easy

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.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
How Can We Check if There is a Loop in a Linked List? Quality Assurance @ IBM Difficulty medium

Describe the method to check if there is a loop in a linked list. Provide the algorithm and example code in Python.

Solution:

Please sign-in to view the solution

Upvote 0 Downvote
Palindrome Program in Java Language Quality Assurance @ IBM Difficulty easy

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).

Solution:

Please sign-in to view the solution