Decode a given pattern in two ways (Flipkart Interview Question)
Last Updated :
31 Jan, 2023
A sender sends a binary string to a receiver meanwhile he encrypt the digits. You are given a encrypted form of string. Now, the receiver needs to decode the string, and while decoding there were 2 approaches.
Let the encrypted binary string be P[] and actual string be S[].
First, receiver starts with first character as 0;
S[0] = 0 // First decoded bit is 1
Remaining bits or S[i]s are decoded using following formulas.
P[1] = S[1] + S[0]
P[2] = S[2] + S[1] + S[0]
P[3] = S[3] + S[2] + S[1]
and so on ...
Second, Receiver starts with first character as 1;
S[0] = 1 // First decoded bit is 1
Remaining bits or S[i]s are decoded using following formulas.
P[1] = S[1] + S[0]
P[2] = S[2] + S[1] + S[0]
P[3] = S[3] + S[2] + S[1]
and so on ...
You need to print two string generated using two different after evaluation from both first and second technique. If any string contains other that binary numbers you need to print NONE.
Input1; 0123210
Output: 0111000, NONE
Explanation for first output
S[0] = 0,
P[1] = S[1] + S[0], S[1] = 1
P[2] = S[2] + S[1] + S[0], S[2] = 1
P[3] = S[3] + S[2] + S[1], S[3] = 1
P[4] = S[4] + S[3] + S[2], S[4] = 0
P[5] = S[5] + S[4] + S[3], S[5] = 0
P[6] = S[6] + S[5] + S[4], S[6] = 0
Explanation for second output
S[0] = 1,
P[1] = S[1] + S[0], S[1] = 0
P[2] = s[2] + S[1] + S[0], S[2] = 1
P[3] = S[3] + S[2] + S[1], S[3] = 2, not a binary character so NONE.
Source: Flipkart Interview | Set 9 (On-Campus)
The idea to solve this problem is simple, we keep track of last two decoded bits. The current bit S[i] can always be calculated by subtracting last two decoded bits from P[i].
Following is the implementation of above idea. We store last two decoded bits in 'first' and 'second'.
C++
#include<iostream>
using namespace std;
// This function prints decoding of P[] with first decoded
// number as 'first'. If the decoded numbers contain anything
// other than 0, then "NONE" is printed
void decodeUtil(int P[], int n, int first)
{
int S[n]; // array to store decoded bit pattern
S[0] = first; // The first number is always the given number
int second = 0; // Initialize second
// Calculate all bits starting from second
for (int i = 1; i < n; i++)
{
S[i] = P[i] - first - second;
if (S[i] != 1 && S[i] != 0)
{
cout << "NONE\n";
return;
}
second = first;
first = S[i];
}
// Print the output array
for (int i = 0; i < n; i++)
cout << S[i];
cout << endl;
}
// This function decodes P[] using two techniques
// 1) Starts with 0 as first number 2) Starts 1 as first number
void decode(int P[], int n)
{
decodeUtil(P, n, 0);
decodeUtil(P, n, 1);
}
int main()
{
int P[] = {0, 1, 2, 3, 2, 1, 0};
int n = sizeof(P)/sizeof(P[0]);
decode(P, n);
return 0;
}
Java
class GFG{
// This function prints decoding of P[]
// with first decoded number as 'first'.
// If the decoded numbers contain anything
// other than 0, then "NONE" is printed
public static void decodeUtil(int P[], int n,
int first)
{
// Array to store decoded bit pattern
int S[] = new int[n];
// The first number is always
// the given number
S[0] = first;
// Initialize second
int second = 0;
// Calculate all bits starting
// from second
for(int i = 1; i < n; i++)
{
S[i] = P[i] - first-second;
if (S[i] != 1 && S[i] != 0)
{
System.out.println("NONE");
return;
}
second = first;
first = S[i];
}
// Print the output array
for(int i = 0; i < n; i++)
{
System.out.print(S[i]);
}
System.out.println();
}
// Driver code
public static void main(String []args)
{
int P[] = { 0, 1, 2, 3, 2, 1, 0 };
int n = P.length;
// This function decodes P[] using
// two techniques 1) Starts with 0
// as first number 2) Starts 1 as
// first number
decodeUtil(P, n, 0);
decodeUtil(P, n, 1);
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# This function prints decoding of P[] with
# first decoded number as 'first'. If the
# decoded numbers contain anything other
# than 0, then "NONE" is printed
def decodeUtil(P, n, first):
S = [0 for i in range(n)]
# array to store decoded bit pattern
S[0] = first # The first number is
# always the given number
second = 0
# Initialize second
# Calculate all bits starting from second
for i in range(1, n, 1):
S[i] = P[i] - first - second
if (S[i] != 1 and S[i] != 0):
print("NONE")
return
second = first
first = S[i]
# Print the output array
for i in range(0, n, 1):
print(S[i], end = "")
print("\n", end = "")
# This function decodes P[] using
# two techniques
# 1) Starts with 0 as first number
# 2) Starts 1 as first number
def decode(P, n):
decodeUtil(P, n, 0)
decodeUtil(P, n, 1)
# Driver Code
if __name__ == '__main__':
P = [0, 1, 2, 3, 2, 1, 0]
n = len(P)
decode(P, n)
# This code is contributed by
# Shashank_Sharma
C#
using System;
class GFG{
// This function prints decoding of P[]
// with first decoded number as 'first'.
// If the decoded numbers contain anything
// other than 0, then "NONE" is printed
static void decodeUtil(int[] P, int n, int first)
{
// Array to store decoded bit pattern
int[] S = new int[n];
// The first number is always
// the given number
S[0] = first;
// Initialize second
int second = 0;
// Calculate all bits starting
// from second
for(int i = 1; i < n; i++)
{
S[i] = P[i] - first - second;
if (S[i] != 1 && S[i] != 0)
{
Console.WriteLine("NONE");
return;
}
second = first;
first = S[i];
}
// Print the output array
for(int i = 0; i < n; i++)
{
Console.Write(S[i]);
}
Console.WriteLine();
}
// Driver code
static public void Main()
{
int[] P = { 0, 1, 2, 3, 2, 1, 0 };
int n = P.Length;
// This function decodes P[] using
// two techniques 1) Starts with 0
// as first number 2) Starts 1 as
// first number
decodeUtil(P, n, 0);
decodeUtil(P, n, 1);
}
}
// This code is contributed by rag2127
PHP
<?php
// This function prints decoding
// of P[] with first decoded
// number as 'first'. If the
// decoded numbers contain anything
// other than 0, then "NONE" is printed
function decodeUtil($P, $n, $first)
{
// The first number is always
// the given number
$S[0] = $first;
// Initialize second
$second = 0;
// Calculate all bits starting
// from second
for ($i = 1; $i < $n; $i++)
{
$S[$i] = $P[$i] - $first - $second;
if ($S[$i] != 1 && $S[$i] != 0)
{
echo "NONE\n";
return;
}
$second = $first;
$first = $S[$i];
}
// Print the output array
for ($i = 0; $i < $n; $i++)
echo $S[$i];
echo "\n";
}
// This function decodes P[]
// using two techniques
// 1) Starts with 0 as first number
// 2) Starts 1 as first number
function decode($P, $n)
{
decodeUtil($P, $n, 0);
decodeUtil($P, $n, 1);
}
// Driver Code
$P=array (0, 1, 2, 3, 2, 1, 0);
$n = sizeof($P);
decode($P, $n);
// This code is contributed by ajit
?>
JavaScript
<script>
// This function prints decoding of P[]
// with first decoded number as 'first'.
// If the decoded numbers contain anything
// other than 0, then "NONE" is printed
function decodeUtil(P, n, first)
{
// Array to store decoded bit pattern
let S = new Array(n);
// The first number is always
// the given number
S[0] = first;
// Initialize second
let second = 0;
// Calculate all bits starting
// from second
for(let i = 1; i < n; i++)
{
S[i] = P[i] - first - second;
if (S[i] != 1 && S[i] != 0)
{
document.write("NONE" + "</br>");
return;
}
second = first;
first = S[i];
}
// Print the output array
for(let i = 0; i < n; i++)
{
document.write(S[i]);
}
document.write("</br>");
}
let P = [ 0, 1, 2, 3, 2, 1, 0 ];
let n = P.length;
// This function decodes P[] using
// two techniques 1) Starts with 0
// as first number 2) Starts 1 as
// first number
decodeUtil(P, n, 0);
decodeUtil(P, n, 1);
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Flipkart Interview Preparation
Flipkart is one of the most well-known and trending e-commerce companies in India, not only because of its market dominance in the Indian e-commerce market, but also because of its open, transparent, and empowering approach to work-life balance. Walmart acquired e-commerce giant Flipkart is currentl
2 min read
Flipkart Interview Experience | Set 18 (For SDE I)
I have recently attended Flipkart for SDE 1 @ Bangalore. Thanks to GeeksforGeeks team which has been the only single source of my preparation and helped me a lot. Below is my experience. Machine Coding Round: Design a book catalogue search (APIâs were given for the search, full needs to be implement
3 min read
Flipkart Interview Experience | Set 17 (For SDE II)
I was interviewed for SDE - II position @ Flipkart. Below is my experience. Round 1 : Telephonic Round (around 90 minutes) He started with his and my introduction, the products I have worked on, the challenges I have faced during its development ( basic questions). Then he asked me 2 questions. Find
3 min read
Flipkart SDE Interview Experience | Set 43 (On-campus for Internship)
Recently Flipkart came to my college for SDE interns. GeeksForGeeks helped me a lot in preparing for the coding tests as well as for interviews. 1st Round. â Online â 60 mins The first round was an online round which was hosted on hackerrank.com . There were 3 Coding questions each of more than aver
2 min read
Flipkart Interview | Set 4 (For SDE-1)
Company: Flipkart Profile: SDE 1 Experience: 7 months Round 1: Online Coding Exam (2 hour on interviewstreet) (Need to pass 4 test cases + if time permits 4 hidden test cases) There are two strings s1 and s2. Find the maximum of percentage word matching from s1 to s2 and s2 to s1. Where, percentage
3 min read
Flipkart Interview Experience | Set 28 (For SDE2)
Round1: Machine coding and discussion based on that. Was asked to design and code snake and ladder game. Then questions where asked on how it can be extended. Round 2: puzzles and data structures Question 1: you are given a 1d array. The values represent the height of wall from floor. We are suppose
1 min read
Flipkart Interview Experience | Set 30 (For SDE 2)
Machine Coding Round: (1 hour) ----------------------------------------- Implement a finite state machine. - The machine should have one start state and can have multiple end states - It should be extensible (I should be able to add any number of states or transitions at any time) - I should be able
2 min read
Flipkart Interview | Set 2 (For SDE 1)
Hi, I had Flipkart interviews for SDE 1. I would like to share my experience. Online Coding Round (1 hour 30 minutes): On interviewstreet, there were 2 problems. It was long description for a DNA problem. Main DNA sequence(a string) is given (let say strDNA) and another string to search for(let say
4 min read
Flipkart Interview Experience | Set 41 (For SDE 1)
Round 1: Machine Coding - 90 mins (You need to code this on the interview site)(Any programming language) Best Selling Price You need to make a program in which 1.) You can add a product -> addProduct("p1") 2.) A user can purchase a product -> purchase("u1","p1") 3.) A user can return a product -> r
2 min read
Flipkart Interview | Set 1 (For SDE 2)
Hi, I have interviewed for Flipkart SDE 2 role 2 months back. I want to share interview experience with other geeks. Telephonic Interview (45 minutes) There is a stream of characters and at any time we need to find and remove (means set occurrence = 0) character which has maximum occurrence till now
3 min read