Monday, December 10, 2018

Q16: Find unique items from the below input

Question 16: Find unique items from the below input.
Input:
[{"name":"test2", "role":"admin","id":2},
{"name":"test1", "role":"guest","id":1},
{"name":"test3", "role":"admin","id":3},
{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2}]

Output:
[{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2},
{"name":"test3", "role":"admin","id":3}]

Monday, November 19, 2018

Q15: Snail shell pattern

Question15: Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

array = [[1,2,3],
         [4,5,6],
         [7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]
For better understanding, please follow the numbers of the next array consecutively:

array = [[1,2,3],
         [8,9,4],
         [7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]
This image will illustrate things more clearly:


NOTE: The 0x0 (empty matrix) is represented as [[]]

Q14: lowest product of 4 consecutive digits in a number

Question 14: Create a function that returns the lowest product of 4 consecutive digits in a number given as a string.

This should only work if the number has 4 digits or more. If not, return "Number is too small".

##Example

lowest_product("123456789")--> 24 (1x2x3x4)
lowest_product("35") --> "Number is too small"

Wednesday, November 14, 2018

Q13: Coloured Triangles

Question 13:A coloured triangle is created from a row of colours, each of which is red, green or blue. Successive rows, each containing one fewer colour than the last, are generated by considering the two touching colours in the previous row. If these colours are identical, the same colour is used in the new row. If they are different, the missing colour is used in the new row. This is continued until the final row, with only a single colour, is generated.

For example, different possibilities are:

Colour here:            G G        B G        R G        B R
Becomes colour here:     G          R          B          G
With a bigger example:

R R G B R G B B
 R B R G B R B
  G G B R G G
   G R G B G
    B B R R
     B G R
      R B
       G
You will be given the first row of the triangle as a string and its your job to return the final colour which would appear in the bottom row as a string. In the case of the example above, you would the given 'RRGBRGBB' you should return 'G'.

The input string will only contain the uppercase letters 'B', 'G' or 'R' and there will be at least one letter so you do not have to test for invalid input.
If you are only given one colour as the input, return that colour.

Monday, November 12, 2018

Q12 : Sum of Digits / Digital Root

A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has two digits, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.

Here's how it works:

digital_root(16)
=> 1 + 6
=> 7

digital_root(942)
=> 9 + 4 + 2
=> 15 ...
=> 1 + 5
=> 6

digital_root(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ...
=> 2 + 4
=> 6

digital_root(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ...
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2

Q11:Cut me in Pieces but in The Way I Like

Question 11:
We need a function (for commercial purposes) that may perform integer partitions with some constraints. The function should select how many elements each partition should have. The function should discard some "forbidden" values in each partition. So, create part_const(), that receives three arguments. part_const((1), (2), (3))

(1) - The integer to be partitioned

(2) - The number of elements that each partition should have

(3) - The "forbidden" element that cannot appear in any partition
part_const() should output the amount of different integer partitions with the constraints required.

Let's see some cases:

part_const(10, 3, 2) ------> 4

/// we may have a total of 8 partitions of three elements (of course, the sum of the elements of each partition should be equal 10) :
[1, 1, 8], [1, 2, 7], [1, 3, 6], [1, 4, 5], [2, 2, 6], [2, 3, 5], [2, 4, 4], [3, 3, 4]

but 2 is the forbidden element, so we have to discard [1, 2, 7], [2, 2, 6], [2, 3, 5] and [2, 4, 4]

So the obtained partitions of three elements without having 2 in them are:
[1, 1, 8], [1, 3, 6], [1, 4, 5] and [3, 3, 4] (4 partitions)///
part_const() should have a particular feature:

if we introduce 0 as the forbidden element, we will obtain the total amount of partitions with the constrained number of elements.

In fact,

part_const(10, 3, 0) ------> 8 # The same eight partitions that we saw above.
Enjoy it and happy coding!!

Q10 : Most frequently used words in a text

Question 10: Write a function that, given a string of text (possibly with punctuation and line-breaks), returns an array of the top-3 most occurring words, in descending order of the number of occurrences.

Assumptions:
A word is a string of letters (A to Z) optionally containing one or more apostrophes (') in ASCII. (No need to handle fancy punctuation.)
Matches should be case-insensitive, and the words in the result should be lowercased.
Ties may be broken arbitrarily.
If a text contains fewer than three unique words, then either the top-2 or top-1 words should be returned, or an empty array if a text contains no words.
Examples:
top_3_words("In a village of La Mancha, the name of which I have no desire to call to
mind, there lived not long since one of those gentlemen that keep a lance
in the lance-rack, an old buckler, a lean hack, and a greyhound for
coursing. An olla of rather more beef than mutton, a salad on most
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
on Sundays, made away with three-quarters of his income.")

# => ["a", "of", "on"]

top_3_words("e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e")

# => ["e", "ddd", "aa"]

top_3_words("  //wont won't won't")

# => ["won't", "wont"]

Bonus points (not really, but just for fun):
Avoid creating an array whose memory footprint is roughly as big as the input text.
Avoid sorting the entire array of unique words.

Sunday, November 11, 2018

Q9 : Sum of Intervals

Question9: Write a function called sumIntervals/sum_intervals() that accepts an array of intervals, and returns the sum of all the interval lengths. Overlapping intervals should only be counted once.

Intervals
Intervals are represented by a pair of integers in the form of an array. The first value of the interval will always be less than the second value. Interval example: [1, 5] is an interval from 1 to 5. The length of this interval is 4.

Overlapping Intervals
List containing overlapping intervals:

[
   [1,4],
   [7, 10],
   [3, 5]
]
The sum of the lengths of these intervals is 7. Since [1, 4] and [3, 5] overlap, we can treat the interval as [1, 5], which has a length of 4.

Examples:
sumIntervals( [
   [1,2],
   [6, 10],
   [11, 15]
] ); // => 9

sumIntervals( [
   [1,4],
   [7, 10],
   [3, 5]
] ); // => 7

sumIntervals( [
   [1,5],
   [10, 20],
   [1, 6],
   [16, 19],
   [5, 11]
] ); // => 19

Friday, November 9, 2018

Q8:Number of trailing zeros of N!

Question8 : Write a program that will calculate the number of trailing zeros in a factorial of a given number.

N! = 1 * 2 * 3 * ... * N

Examples
zeros(6) = 1
# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero

zeros(12) = 2
# 12! = 479001600 --> 2 trailing zeros

Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros.

Q7:Reverse every other word in the string

Question 7: Reverse every other word in a given string, then return the string. Throw away any leading or trailing whitespace, while ensuring there is exactly one space between each word. Punctuation marks should be treated as if they are apart of the word.

Sample 1:
Input: "I really hope it works this time..."
Output: "I yllaer hope ti works siht time..."

Sample 2:
Input: "Reverse this string, please!"
Output : "Reverse siht string, !esaelp"

Thursday, November 8, 2018

Q6: Find the pair in sorted list whose sum is closest or same as value of x

Question 6: Given a sorted array and a number x, find the pair in array whose sum is closest to or same as x
 Examples:
 Input: [2, 10, 22, 28, 29, 30, 40,51], x = 54
 Output: 2 and 51
 Input: [1, 3, 5, 7, 10], x = 15
 Output: 5 and 10

Tuesday, October 30, 2018

Q5: Replace consecutive occurrences of the character from given input string

Question 5: Replace consecutive occurrences of the character from given input string.
You are given a string S. If a character 'c' occurs consecutively N times in the string. Replace these consecutive occurrences of the character 'c' with (N,c) in the string.

Eg:
Input: "1222311"
Output: "(1, 1) (3, 2) (1, 3) (2, 1)"

Here, character 1 occurs only once. It is replaced by (1,1). Then the character 2 occurs 3 times, and it is replaced by (3,2) and so on.

Q4: Write a logic to verify the given input string is correctly balanced

Question 4: Write a logic to verify the given input string is correctly balanced

Example: 

Input: "[{()}]"
Output: Balanced 

Input: "{(}([)])"
Output: Not Balanced 

Input: "({()})"
Output: Balanced 

Input: "[{(){[( )]}}]"
Output: Balanced 

Q3: A stream of data is received , split it to bytes and needs to be reversed

Question: A stream of data is received and needs to be reversed. Each segment is 8 bits meaning the order of these segments need to be reversed:

11111111 00000000 00001111 10101010

(byte1) (byte2) (byte3) (byte4)

10101010 00001111 00000000 11111111

(byte4) (byte3) (byte2) (byte1)

Total number of bits will always be a multiple of 8. The data is given in an array (flat list) as such:

[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0] 

And output should be like below (flat list)
[1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]


Q2: Highest scoring word in a string of words

Question : Given a string of words, you need to find the highest scoring word.

Each letter of a word scores points according to it's position in the alphabet: a = 1, b = 2, c = 3 etc.

You need to return the highest scoring word as a string.

If two words score the same, return the word that appears earliest in the original string.

All letters will be lowercase and all inputs will be valid.

eg:

string = "Hi this is tish"
Ranks for each word will be like: [17, 56, 28, 56]
And highest rand words are this and tish. But "this" is the answer since it is first occurrence.

Q1: Reverse a string without affecting special characters

Question 1: Reverse a string without affecting special characters
  

Note that $ and , are not moved anywhere. Only sub sequence "abc" is reversed.

Eg 1:
    Input:   str = "a,b$c"
   Output:  str = "c,b$a"
 


Eg 2:
    Input:   str = "Ab,c,de!$"
    Output:  str = "ed,c,bA!$"