# ByteDance Interview Questions  Top 45 Ruby interview questions for 2021

## Array Questions ByteDance

Question 1. Combination Sum Leetcode Solution The problem Combination Sum Leetcode Solution provides us an array or list of integers and a target. We are told to find the combinations that can be made using these integers any number of times that add up to the given target. So more formally, we can use the given ...

Question 2. Maximum Subarray Leetcode Solution Problem Statement   Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example nums = [-2,1,-3,4,-1,2,1,-5,4] 6 Explanation: [4,-1,2,1] has the largest sum = 6. nums = [-1] -1 Approach 1 (Divide and Conquer)   In this approach ...

Question 3. Unique Paths Leetcode Solution The problem Unique Paths Leetcode Solution states that you are given two integers representing the size of a grid. Using the size of the grid, the length, and breadth of the grid. We need to find the number of unique paths from the top left corner of the grid to ...

Question 4. Merge Sorted Arrays Leetcode Solution In the problem “Merge Sorted Arrays”, we are given two arrays sorted in non-descending order. The first array is not fully filled and has enough space to accommodate all elements of the second array as well. We have to merge the two arrays, such that the first array contains elements ...

Question 5. Search in Rotated Sorted Array Leetcode Solution Consider a sorted array but one index was picked and the array was rotated at that point. Now, once the array has been rotated you are required to find a particular target element and return its index. In case, the element is not present, return -1. The problem is generally ...

Question 6. Kth largest element in an Array Leetcode Solutions In this problem, we have to return the kth largest element in an unsorted array. Note that the array can have duplicates. So, we have to find the Kth largest element in the sorted order, not the distinct Kth largest element. Example   A = {4 , 2 , 5 , 3 ...

Question 7. Find First and Last Position of Element in Sorted Array Leetcode Solution Problem statement   In this article titled “Find First and Last Position of Element in Sorted Array Leetcode Solution,” we will discuss the solution to a leetcode problem. In the given problem we are given an array. We are also given a target element. Elements in the array are sequenced in ...

Question 8. Count all subsequences having product less than K The problem “Count all subsequences having product less than K” states that you are given an array of integers. Now find the number of subsequences that have a product less than a given input K. Example   a[] = {1, 2, 3, 4, 5} k = 8 Number of subsequences less ...

Question 9. Print modified array after executing the commands of addition and subtraction You are given an array of size n, initially all the values in the array will be 0, and the queries. Each query contains the four values, type of the query T, left point of the range, the right point of a range and a number k, you have to ...

Question 10. Best Time to Buy and Sell Stock Problem Statement   The problem “Best Time to Buy and Sell Stock” states that you are given an array of prices of length n, where the ith element stores the price of stock on ith day. If we can make only one transaction, that is, to buy on one day and ...

Question 11. Top K Frequent Elements Problem Statement   In top K frequent elements we have given an array nums[], find the k most frequently occurring elements. Examples   nums[] = {1, 1, 1, 2, 2, 3} k = 2 1 2   nums[] = {1} k = 1 1 Naive Approach for Top K Frequent Elements   Build ...

Question 12. Sum of minimum and maximum elements of all subarrays of size k Problem Statement   The problem “Sum of minimum and maximum elements of all subarrays of size k” states that you are given an array containing positive and negative integers, find the sum of minimum and maximum elements of all the sub-arrays of size k. Examples   arr[] = {5, 9, 8, 3, ...

Question 13. Minimum number of distinct elements after removing m items Problem Statement   The problem “Minimum number of distinct elements after removing m items” states that you have an array and an integer m. Each element of the array indicates an item id’s. The problem statement asks to remove m elements in such a way that there should be a minimum ...

Question 14. Subset Leetcode In Subset Leetcode problem we have given a set of distinct integers, nums, print all subsets (the power set). Note: The solution set must not contain duplicate subsets. An array A is a subset of an array B if a can be obtained from B by deleting some (possibly, zero ...

Question 15. Word Search Word search is something like the word-finding puzzles at some time in our life. Today I bring to the table a modified crossword. My readers must be a bit perplexed as to what I am talking about. Without wasting any more time let us get to the problem statement Can ...

Question 16. Median of Two Sorted Arrays Given two sorted arrays A and B of size n and m respectively. Find the median of the final sorted array obtained after merging the given two arrays or in other words, we say that find median of two sorted arrays. ( Expected time complexity: O(log(n)) ) Approach 1 for ...

Question 17. Search an Element in Sorted Rotated Array In search in sorted rotated array problem we have given a sorted and rotated array and an element, check if the given element is present in the array or not. Examples   Input nums[] = {2, 5, 6, 0, 0, 1, 2} target = 0 Output true Input nums[] = {2, ...

Question 18. Search in Sorted Rotated Array An element search in sorted rotated array can be found using binary search in O(logn) time. The objective of this post is to find a given element in a sorted rotated array in O(logn) time. Some example of a sorted rotated array is given. Example   Input : arr[] = {7,8,9,10,1,2,3,5,6}; ...

Question 19. Maximum Subarray In the Maximum Subarray problem we have given an integer array nums, find the contiguous sub array which has the largest sum and print the maximum sum subarray value. Example   Input nums[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4} Output 6 Algorithm   The goal is to find ...

Question 20. Find Peak Element Let’s understand Find Peak Element problem. Today we have with us an array that needs its peak element. Now, you must be wondering as to what do I mean by the peak element? The peak element is one which is greater than all its neighbours. Example: Given an array of ...

Question 21. Coin Change Problem Coin Change Problem – Given some coins of different values c1, c2, … , cs (For instance: 1,4,7….). We need an amount n. Use these given coins to form the amount n. You can use a coin as many times as required. Find the total number of ways in which ...

Question 22. Maximum Subarray Sum using Divide and Conquer Problem Statement   In the “Maximum Subarray Sum using Divide and Conquer” problem we have given an array of both positive and negative integers. Write a program that will find the largest sum of the contiguous subarray. Input Format   The first line containing an integer N. Second-line containing an array of ...

Question 23. Arrange given Numbers to Form the Biggest Number II Problem Statement   In the “Arrange given Numbers to Form the Biggest Number II” problem, we have given an array of positive integers.  Arrange them in such a way that the arrangement will form the largest value. Input Format   The first and only one line containing an integer n. Second-line containing ...

Question 24. Maximum Sum Increasing Subsequence Problem Statement   In the “Maximum Sum Increasing Subsequence” problem we have given an array. Find the sum of the maximum subsequence of the given array, that is the integers in the subsequence are in sorted order. A subsequence is a part of an array which is a sequence that is ...

Question 25. Find the Peak Element from an Array Problem Statement   In the “Find the Peak Element from an Array” problem we have given an input array of integers. Find a peak element. In an array, an element is a peak element, if the element is greater than both the neighbours. For corner elements, we can consider the only ...

Question 26. Partition Problem Problem Statement   In the Partition problem, we have given a set that contains n elements. Find whether the given set can be divided into two sets whose sum of elements in the subsets is equal. Example   Input arr[] = {4, 5, 11, 9, 8, 3} Output Yes Please click Like ...

Question 27. Subarray with Given Sum Problem Statement   In the subarray with the given sum problem, we have given an array containing n positive elements. We have to find the subarray in which the sum of all the elements of the subarray equal to a given_sum. Subarray is obtained from the original array by deleting some ...

Question 28. Merge Two Sorted Arrays Problem Statement   In merge two sorted arrays problem, we have given two input sorted arrays, we need to merge these two arrays such that the initial numbers after complete sorting should be in the first array and remaining in the second array. Example   Input A[] = {1, 3, 5, 7, ...

Question 29. Count of Triplets With Sum Less than Given Value Problem Statement   We have given an array containing N number of elements. In the given array, Count the number of triplets with a sum less than the given value. Example   Input a[] = {1, 2, 3, 4, 5, 6, 7, 8} Sum = 10 Output 7 Possible triplets are : ...

Question 30. Merging Two Sorted Arrays Problem Statement   In merging two sorted arrays problem we have given two sorted arrays, one array with size m+n and the other array with size n. We will merge the n sized array into m+n sized array and print the m+n sized merged array. Example   Input 6 3 M[] = ...

Question 31. Find Triplet in Array With a Given Sum Problem Statement   Given an array of integers, find the combination of three elements in the array whose sum is equal to a given value X. Here we will print the first combination that we get. If there is no such combination then print -1. Example   Input N=5, X=15 arr[] = ...

Question 32. Smallest Positive Number Missing in an Unsorted Array Problem Statement   In the given unsorted array find the smallest positive number missing in an unsorted array. A positive integer doesn’t include 0. We can modify the original array if needed. The array may contain positive and negative numbers. Example   a. Input array : [3, 4, -1, 0, -2, 2, 1, ...

Question 33. Move All the Zeros to the End of the Given Array Problem Statement   In the given array move all the zeros which are present in the array to the end of the array. Here there is always a way exist to insert all the number of zeroes to the end of the array. Example   Input 9 9 17 0 14 0 ...

Question 34. Count Number of Occurrences in a Sorted Array Problem Statement   In the “Count Number of Occurrences in a Sorted Array” problem, we have given a sorted array. Count the number of occurrences or frequency in a sorted array of X where X is an integer. Example   Input 13 1 2 2 2 2 3 3 3 4 4 ...

Question 35. A Product Array Puzzle Problem Statement   In a product array puzzle problem we need to construct an array where the ith element will be the product of all the elements in the given array except element at the ith position. Example   Input  5 10 3 5 6 2 Output Please click Like if you ...

Question 36. Majority Element Problem Statement   Given a sorted array, we need to find the majority element from the sorted array. Majority element: Number occurring more than half the size of the array. Here we have given a number x we have to check it is the majority_element or not. Example   Input 5 2 ...

## String Questions ByteDance

Question 37. Multiply Strings Leetcode Solution The problem Multiply Strings Leetcode solution asks us to multiply two strings which are given to us as input. We are required to print or return this result of multiplying to the caller function. So to put it more formally given two strings, find the product of the given strings. ...

Question 38. Longest Repeated Subsequence The problem “Longest Repeated Subsequence” states that you are given a string as an input. Find out the longest repeated subsequence, that is the subsequence that exists twice in the string. Example   aeafbdfdg 3 (afd) Approach   The problem asks us to find out the longest repeated subsequence in the string. ...

Question 39. Longest Substring Without Repeating Characters Given a string, we have to find the length of the longest substring without repeating characters. Let’s look into a few examples: Example   pwwkew 3 Explanation: Answer is “wke” with length 3 aav 2 Explanation: Answer is “av” with length 2 Approach-1 for Longest Substring Without Repeating Characters    Brute Force  ...

Question 40. Palindrome Substring Queries Problem Statement   The problem “Palindrome Substring Queries” states that you are given a String and some queries. With those queries, you have to determine if the formed substring from that query is a palindrome or not. Example   String str = "aaabbabbaaa" Queries q[] = { {2, 3}, {2, 8},{5, 7}, ...

Question 41. Maximum weight transformation of a given string Problem Statement   The maximum weight transformation of a given string problem states that given a string consisting only of two characters ‘A’ and ‘B’. We have an operation where we can transform string to another string by toggling any character. Thus many transformations are possible. Out of all the possible ...

Question 42. Edit Distance In the edit distance problem we have to find the minimum number of operations required to convert a string X of length n to another string Y of length m. Operations allowed: Insertion Deletion Substitution Example   Input: String1 = “abcd” Please click Like if you loved this article? String2 = ...

Question 43. Decode String Suppose, you are given an encoded string. A string is encoded in some kind of pattern, your task is to decode the string. Let us say, < no of times string occurs > [string ] Example   Input 3[b]2[bc] Output Please click Like if you loved this article? bbbcaca Explanation Here ...

Question 44. Next Permutation In the next permutation problem we have given a word, find the lexicographically greater_permutation of it. Example   input : str = "tutorialcup" output : tutorialpcu input : str = "nmhdgfecba" output : nmheabcdfg input : str = "algorithms" output : algorithsm input : str = "spoonfeed" output : Next Permutation ...

Question 45. Valid Parentheses In Valid Parentheses problem we have given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. ( ) [ ] { } ...

Question 46. Permutations of a Given String Using STL Problem Statement   In the “Permutations of a Given String Using STL” problem, we have given a string “s”. Print all the permutations of the input string using STL functions. Input Format   The first and only one line containing a string “s”. Output Format   Print all the permutation of the given ...

Question 47. Length of Longest valid Substring Problem Statement   In the “Length of Longest valid Substring” we have given a string that contains the opening and closing parenthesis only. Write a program that will find the longest valid parenthesis substring. Input Format   The first and only one line containing a string s. Output Format   The first and ...

Question 48. Arrange given Numbers to Form the Biggest Number II Problem Statement   In the “Arrange given Numbers to Form the Biggest Number II” problem, we have given an array of positive integers.  Arrange them in such a way that the arrangement will form the largest value. Input Format   The first and only one line containing an integer n. Second-line containing ...

## Tree Questions ByteDance

Question 49. Minimum number of distinct elements after removing m items Problem Statement   The problem “Minimum number of distinct elements after removing m items” states that you have an array and an integer m. Each element of the array indicates an item id’s. The problem statement asks to remove m elements in such a way that there should be a minimum ...

Question 50. Convert BST to Min Heap Problem Statement   Given a complete Binary Search Tree, write an algorithm to convert it into a Min Heap, which is to convert BST to Min Heap. The Min Heap should be such that the values on the left of a node must be less than the values on the right ...

Question 51. Convert a normal BST to Balanced BST Problem Statement   Given a Binary Search Tree(BST), write an algorithm to convert the BST to a Balanced Binary Search Tree. A balanced Binary Search tree is nothing but a binary search tree whose difference between the height of left subtree and right subtree is less than or equal to 1. ...

Question 52. Construct Binary Tree from Given Inorder and Preorder Traversals In this problem, we have inorder and preorder of the binary tree. We need to construct a binary tree from the given Inorder and Preorder traversals. Example   Input: Inorder= [D, B, E, A, F, C] Preorder= [A, B, D, E, C, F] Output: Pre-order traversal of the tree formed by ...

Question 53. Recover Binary Search Tree Consider a binary search tree, two nodes of the tree have been swapped, design an algorithm to recover the binary search Tree. Example Consider the binary search tree given below whose two nodes have been swapped as input. Incorrect nodes on the BST are detected(highlighted) and then swapped to obtain ...

Question 54. Validate Binary Search Tree Problem   In Validate Binary Search Tree problem we have given the root of a tree, we have to check if it is a binary search tree or not. Example : Output: true Please click Like if you loved this article? Explanation: The given tree is a binary search tree because ...

## Stack Questions ByteDance

Question 55. Decode String Suppose, you are given an encoded string. A string is encoded in some kind of pattern, your task is to decode the string. Let us say, < no of times string occurs > [string ] Example   Input 3[b]2[bc] Output Please click Like if you loved this article? bbbcaca Explanation Here ...

## Queue Questions ByteDance

Question 56. Sum of minimum and maximum elements of all subarrays of size k Problem Statement   The problem “Sum of minimum and maximum elements of all subarrays of size k” states that you are given an array containing positive and negative integers, find the sum of minimum and maximum elements of all the sub-arrays of size k. Examples   arr[] = {5, 9, 8, 3, ...

Question 57. Queue Reconstruction by Height Problem Description of Queue Reconstruction by Height   Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person ...

## Matrix Questions ByteDance

Question 58. Word Search Leetcode Solution Problem Statement   Given an m x n board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where “adjacent” cells are horizontally or vertically neighbouring. The same letter cell may not be used more than once. Example ...

## Other Questions ByteDance

Question 59. Permutations Leetcode Solution The problem Permutations Leetcode Solution provides a simple sequence of integers and asks us to return a complete vector or array of all the permutations of the given sequence. So, before going into solving the problem. We should be familiar with permutations. So, a permutation is nothing but an arrangement ...

Question 60. Two Sum Leetcode Solution In this problem, we have to find a pair of two distinct indices in a sorted array that their values add up to a given target. We can assume that the array has only one pair of integers that add up to the target sum. Note that the array is ...

Question 61. Lexicographical Numbers Leetcode Solution Problem statement   In the problem ” Lexicographical Numbers” we are given a number n. Our task is to print numbers between 1 and n in lexicographical order. Example n=13 [1 10 11 12 13 2 3 4 5 6 7 8 9] Explanation: As we have to print numbers between ...

Question 62. Maximum number of segments of lengths a, b and c The problem “Maximum number of segments of lengths a, b and c” states that you are given a positive integer N, and you need to find the maximum number of segments of lengths a,b, and c that can be formed using N. Example   N = 7 a = 5, b ...

Question 63. A Space Optimized DP solution for 0-1 Knapsack Problem Problem Statement   We are given a knapsack which can hold some weight, we need to pick some of the items out of given items with some value. The items should be picked such that the value of the knapsack ( total value of picked up items ) should be maximized. ...

Question 64. K-th Distinct Element in an Array You are given an integer array A, print k-th distinct element in an array. The given array may contain duplicates and the output should print k-th distinct element among all unique elements in an array. If k is more than a number of distinct elements, then report it. Example   Input: ...

Question 65. Intersection of Two Arrays In intersection of two arrays problem, we have given two arrays, we need to print their intersection(common elements). Example   Input arr1[] = {1, 2, 2, 1} arr2[] = {2, 2} Output {2, 2} Input arr1 = {4, 9, 5} arr2 = {9, 4, 9, 8, 4} Output {4, 9} Algorithm ...

Question 66. Leetcode Permutations In this leetcode problem premutation we have given an array of distinct integers, print all of its possible permutations. Examples   Input arr[] = {1, 2, 3} Output 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Input arr[] = {1, 2, ...

Question 67. Merge K Sorted Linked Lists Merge K sorted linked lists problem is so famous as per the interview point of view. This question asks so many times in big companies like Google, Microsoft, Amazon, etc.  As the name suggests we’ve been provided with k sorted linked lists. We have to merge them together into a ...

Question 68. Find Median from data Stream In Find Median from the data Stream problem, we have given that integers are being read from a data stream. Find the median of all the elements read so far starting from the first integer till the last integer. Example   Input 1: stream[ ] = {3,10,5,20,7,6} Output : 3 6.5 ...

Question 69. Sliding Window Maximum In Sliding Window Maximum problem we have given an array nums, for each contiguous window of size k, find the maximum element in the window. Example   Input nums[] = {1,3,-1,-3,5,3,6,7} k = 3 Output {3,3,5,5,6,7} Explanation Naive Approach for Sliding Window Maximum   For every contiguous window of size k, traverse ...

Question 70. Word Break Word Break is a problem that beautifully illustrates a whole new concept. We have all heard of compound words. Words made up of more than two words. Today we have a list of words and all we’ve got to do is check if all the words from the dictionary can ...

Question 71. Reverse Nodes in K-Group Problem   In Reverse Nodes in K-Group problem we have given a linked list, Reverse the linked list in a group of k and return the modified list. If the nodes are not multiple of k then reverse the remaining nodes. The value of k is always smaller or equal to ...

Question 72. LRU Cache Implementation Least Recently Used (LRU) Cache is a type of method which is used to maintain the data such that the time required to use the data is the minimum possible. LRU algorithm used when the cache is full. We remove the least recently used data from the cache memory of ...

Question 73. N queen problem N queen problem using the concept of Backtracking. Here we place queen such that no queen under attack condition. The attack condition of the queens is if two queens are on the same column, row, and diagonal then they are under attack. Let’s see this by the below figure. Here ...