Posts

Showing posts with the label Placement Questions

Find Maxima/Minima

 Coding Problem Keys   Find Maxima/Minima   Problem Statement  Implement the following function: def MaximaOrMinima(a, b, c): Quadratic equation: A quadratic equation is any equation having the form, ax ² + bx + c, where 'a' cannot be zero. The function accepts coefficients of a quadratic equation, 'a', 'b' and 'c' as its argument. Implement the function to find the maximum or minimum value of quadratic equation by substituting integer values x, where -100 <= x <= 100. Return the values as follow: If a > 0, return the minimum value of the equation. If a < 0, return the maximum value of the equation. Assumption: a is not equal to zero. Note: Computed value lies within integer range.  Example 1   Input  a: 1 b: 2 c: 1  Output  0  Explanation  Since, (a > 0) output is the minimum value which is at x = -1, output = 1 * (-1) ²  + 2 * (-1) + 1 = 0. The custom input format for the above case: 1 2 1 (Th...

Occurrences of Sub in Parent

 Coding Problem Keys   Occurrences of Sub in Parent   Problem Statement  You are given two strings containing only English letters. Write an algorithm to count the number of occurrences of the second string in the first string. (You may disregard the case of the letters).  Input  The first line of the input consists of a string parent, representing the first string. The second line consists of a string sub, representing the second string.  Output  Print an integer representing the number of occurrences of Sub in Parent. If no occurrence of Sub is found in Parent then print 0.  Example 1   Input  TimisplayinginthehouseofTimwiththetoysofTim Tim  Output  3  Explanation  Tim occurs 3 times in the first string, So, the output is 3.  Example 2   Input  abcdefg bcd  Output  1  Explanation  Tim occurs 1 times in the first string, So, the output is 1.  Solution...

Desktop Products

 Coding Problem Keys   Desktop Products   Problem Statement  A company DigiComparts manufactures 52 types of unique products for laptop and desktop computers. It manufactures 10 types of laptop products and 42 types of desktop products. Each product manufactured by the company has a unique productID from a-z and and A-Z. The laptop products have productIDs (a, i, e, o, u, A, I, E, O, U) while the rest of the productIDs are assigned to the desktop products. The company manager wishes to find the sales data for the desktop products. Given a list of productIDs of the sales of the last N products, write an algorithm to help the manager find the productIDs of the desktop products.  Input Format  The first line of the input consists of an integer numOfProducts, representing the number of products to be considered in the sales data (N). The second line consists of N space-separated characters - productID 1 , productID 2 , ... , productID N  represen...

Minimum Operations in Adjacent Elements

 Coding Problem Keys   Minimum Operations in Adjacent Elements   Problem Statement  You are given an array A of N distinct numbers. In one operation, you can choose two adjacent elements and remove the minimum one. Find the minimum number of operations needed to make the array in increasing order.  Input Format  The first line contains an integer, N, denoting the number of elements in A. Each line i of the N subsequent lines (where 0 <= i <= N) contains an integer describing A[i].  Output Format  The output prints the minimum number of operations needed to make the array to be in increasing order.  Constraints  1 <= N <= 10 ⁵ 1 <= A[i] <= 10 ⁹  Example Input/Output 1   Input  2 8 10  Output  0  Explanation  We don't have to remove any element as it is already in increasing order.  Example Input/Output 2   Input  3 10 3 4  Output  2  Not...

Matrix - Sum of Minor Diagonal

 Coding Problem Keys   Matrix - Sum of Minor Diagonal   Problem Statement  Maria, class teacher of standard twelve has set out on a vacation to Coorg. They have booked accommodation in a resort and the resort authorities organize Campfires every night as a part of their daily activities. But she can't take all the students of her class on vacation. So she planned to keep a test, those who score full marks on the test are eligible to go on a vacation. The test consists of a single question based on a 2D array, Given a square matrix x[5][5], write a code to compute the sum of minor diagonal. Help her to solve this.  Input Format  The input consists of a 5*5 square matrix.  Output Format  The output prints the sum of minor diagonal.  Example Input/Output 1   Input  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  Output  65  Explanation  The sum of minor diagonal elements, 5 + 9 + 13...

Smallest String Possible

 Practice Questions   Smallest String Possible   Problem Statement  You are given two string S and T each of length N and K pairs of integers L ᵢ  and R ᵢ  ( 0 ≤ L ᵢ  < R ᵢ  ≤ N - 1) You can perform any of the two operations any number of times. You can replace the character of the string S at the i-th a position with the character of string T at i-th position. You can choose from any given K pairs and You are allowed to swap characters at the position L ᵢ  and R ᵢ  in the string T. Now you have to perform all the operations optimally so that the string S can be lexicographically smallest. http://en.wikipedia.org/wiki/Lexicographical_order Note: All characters of S and T are lower case English letters. [a - z].  Input Format  The first line will contain the number of test cases. For each test case, the next line will contain N and K, the length of the string and the number of pairs of integers. The n...

Maximum String Score

 Practice Questions   Maximum String Score   Problem Statement  You are given a list S that contains strings. Note: Each string in the list consists of only lowercase Latin Letters. The score of any string P is represented as follows. The sum of the number of strings in S in which P occurs as a substring and the number of strings in S in which P occurs as a prefix.  Example  If S = [ 'abc', 'abab', 'aba' ] and P = 'ab', then score of P = 3. This is because (P occurs in all the strings) + 3 (P occurs in 'abc', 'abab', and 'aba' as a prefix) = 6. Note: If P occurs as a prefix in any of the strings, then it will also contribute to the substring. Your task is to determine the maximum score possible of any string provided.  Input Format  The first line contains T denoting the number of test cases. For each test case The first line contains the N denoting the number of strings in the list. The Next N lines contain the elements of the ...

Perfect Sums

 Coding Problem Keys   Perfect Sums   Problem Statement  A Perfect sum is the sum of two or more elements of an array which is equal to a given number. Your task is to write a function to compute the number of perfect sums existing in an array. Note: If no such sum exits, return 999. Also, if the input array is not an integer array, then also return 999.  Input Format  input1: An integer value representing the length of the integer array. input2: An integer array of length N. input3: An integer value representation the number, the perfect sum must be equal to.  Output Format  Return the number of perfect sums existing in the array.  Example 1   Input  input1: 5 input2: {9, 7, 3, 12, 7} input3: 14  Output  output: 1  Explanation  Here, the sum of elements 7 + 7 = 14. Hence the number of perfect sums in the given array is 1. Therefore, 1 is returned as the output.  Example 2   Input...

Max Contiguous Sequences

 Coding Problem Keys   Max Contiguous Sequences   Problem Statement  Given an array of N integers, determine all the contiguous subsequences of positive numbers. Then write a function to find the sum of elements in each sub-sequence and output the maximum sum values.  Boundary Condition(S)  1 <= R <= N <= 100 1 <= ID of each item <= 1000  Input Specification  input1: An integer N denoting the length of the array input2: An integer array of length N (-10000 <= A[i] <= 10000)  Output Specification  Return the maximum sum of contiguous sub-sequence of numbers.  Example Input/Output 1   Input  input1: 5 input2: {1, 2, 4, -2, 3}  Output  output: 7  Explanation  Contiguous sub-sequence of positive numbers in the given array are {1, 2, 4} and {3}. Their respective sums are 7 and 3. Hence, the maximum value between them is 7. Hence the output is 7.  Solution  ...

Electrostatic

 Coding Problem Keys   Electrostatics   Problem Statement  Doug is fond of change, every now and then he tries to do new things. This time, he caught up with a rod comprising of negative (N) and positive (P) charges. He is asked to calculate the maximum net electrostatic field possible in the region due to the rod. Note: Assume, Electrostatic Field = Total Charge * 100.  Input Specification  Input1:  integer array denoting the magnitude of each charge Input2: String denoting the nature of each charge, i-th entry represents a sign of charge at the i-th loccation in input1. Input3: No. of charges it holds (Length of input1).  Output Specification  Return the maximum electrostatic field possible in the rod.  Example Input/Output 1   Input  Input1: {4,3,5} Input2: PNP Input3: 3   Output  Output:  600   Explanation  The maximum electric charge on the rod is 4 - 3 + 5 = 6 units, so the mag...

Sum of Numbers - XOR and OR

 Coding Problem Keys   Sum of Numbers - XOR and OR   Problem Statement  In the faculty of Informatics Engineering, the Teacher of Mathematics gave a lecture on bits and explained XOR and OR. Then he decided to test the competence of his students by giving them this question. He will give the students an array A of N numbers and then give them Q queries. Each query will consist of three numbers Type ᵢ , L ᵢ  and R ᵢ . Each of the queries can be one of the following types.     - If Type ᵢ is 1, then the student has to replace each A ᵢ with (A ᵢ | (A ᵢ - 1)) for every i between L ᵢ and R ᵢ.     - If Type ᵢ is 2, this means that the student must replace each A ᵢ with (A ᵢ ^ (A ᵢ - 1)) for each i between the L ᵢ and R ᵢ. After each query, the professor asks his students to calculate the sum of the numbers from array A. You need to find the sum of the answers to all the queries modulo 10^9+7. Note The sign | represents the logical OR....