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!$"