Skip to main content

GOOGLE PROGRAMMING QUESTION

@V

google program

Try to Solve it by your self I can give you Solution after a 2 Day's.. on my Solution this Page.

QUESTION OF THE WEEK :-

QUESTION 5 :-
Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.

CODE :-
#include <stdio.h> 
#include <string.h> 
const char hashTable[10][5] = {"", "", "abc", "def", "ghi", "jkl","mno", "pqrs", "tuv", "wxyz"}; 
void printWordsUtil(int number[], int curr_digit, char output[], int n) 

int i; 
if (curr_digit == n) 

printf("%s ", output); 
return ; 
 
for (i=0; i<strlen(hashTable[number[curr_digit]]); i++) 

output[curr_digit] = hashTable[number[curr_digit]][i]; 
printWordsUtil(number, curr_digit+1, output, n); 
if (number[curr_digit] == 0 || number[curr_digit] == 1) 
return; 

 
void printWords(int number[], int n) 

char result[n+1]; 
result[n] ='\0'; 
printWordsUtil(number, 0, result, n); 
 
int main(void) 

int number[] = {2, 3, 4}; 
int n = sizeof(number)/sizeof(number[0]); 
printWords(number, n); 
return 0; 







Question 4 :-
Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters.
For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”.

CODE :-
#include <iostream>
#include <sstream>
#include <deque>

inline auto read7()
{
  static std::istringstream is("1234567890123456789012345678901234567890");
  char res[8]{};
  is.get(res, 8);
  return std::string(res);
}

template <typename T1, typename T2>
auto& operator<<(std::deque<T1>& deq, const T2& seq)
{
  for (const auto& elm : seq)
  {
    deq.push_back(elm);
  }
  return deq;
}

template <typename T1, typename T2>
auto get(std::deque<T2>& deq, std::size_t n)
{
  T1 res;
  while (res.size() != n && !deq.empty())
  {
    res += deq.front();
    deq.pop_front();
  }
  return res;
}

auto readN(std::size_t n)
{
  static std::deque<char> buffer;
  while (n > buffer.size())
  {
    auto old_sz = buffer.size();
    buffer << read7();
    if (old_sz == buffer.size())
      break;
  }

  return get<std::string>(buffer, n);
}











Question 3:-

Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word. If there is more than one palindrome of minimum length that can be made, return the lexicographically earliest one (the first one alphabetically).

For example, given the string "race", you should return "ecarace", since we can add three letters to it (which is the smallest amount to make a palindrome). There are seven other palindromes that can be made from "race" by adding three letters, but "ecarace" comes first alphabetically.

As another example, given the string "google", you should return "elgoogle".



Question 2 :-

Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].



FIRST TRY IT YOUR SELF THAN SEE..


If you find Solution in other language comment it comment section Give your Contribution and show your capabilities..




Code Solution :-


def sort(rgbs):
left_index, right_index = 0, len(rgbs) - 1
while True: # move Rs to front
while rgbs[left_index] == 'R' and left_index < right_index: # advance to first non R
left_index += 1
while rgbs[right_index] != 'R' and left_index < right_index: # regress to last R
right_index -= 1
if left_index >= right_index:
break
rgbs[left_index], rgbs[right_index] = rgbs[right_index], rgbs[left_index]
right_index = len(rgbs) - 1
while True: # move Bs to tail
while rgbs[left_index] != 'B' and left_index < right_index: # advance to first B
left_index += 1
while rgbs[right_index] == 'B' and left_index < right_index: # regress to last non B
right_index -= 1
if left_index >= right_index:
break
rgbs[left_index], rgbs[right_index] = rgbs[right_index], rgbs[left_index]
return rgbs
def main():
print sort(['G', 'B', 'R', 'R', 'B', 'R', 'G'])
if __name__ == '__main__':
main()
link to follow me :- httpgram.com/abhishek._.yadav._/?hl=ens://www.insta


EASY_LEVEL

Question 1:-
Given a array of numbers representing the stock prices of a company in chronological order, write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy before you can sell it.
For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could buy the stock at 5 dollars and sell it at 11 dollars.

FIRST TRY IT YOUR SELF THAN SAW MY SOLUTION


CODE :-

#include<iostream>
using namespace std;
int main(){
int n;  //size of an array
int j;
cout<<" Size of an Array :- ";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];   //insert an array 
}
// purchase stock minimum
int t; // min stock
t=a[0];
for(int i=1;i<n;i++){
if(t>a[i]){
t=a[i];
}
}
// sale max price
int t1;
t1=a[0];
for(int i=0;i<n;i++){
if(a[i]>t1){
t1=a[i];
}
}
cout<<"buy minimum price stock :- "<<t<<endl;
cout<<"sale maximum price stock :- "<<t1<<endl;
cout<<"profit on stock :- "<<t1-t<<endl;
return 0;

}

INPUT :- 
6
9 11 8 5 7 10

OUTPUT :-
buy minimum price stock :- 5
buy maximum price stock :- 11
profit on stock :- 6

FOLLOW ME ON INSTAGRAM
 httpgram.com/abhishek._.yadav._/?hl=ens://www.insta

Comments

Popular posts from this blog

Byte Code:

Java Programming Byte Code: It is the unique characteristic property of the Java programming language. It is something like a normal text file. Therefore, it cannot be affected by virus. It is an intermediate between a human readable source and machine readable source. Byte codes are plate form independent. Therefore, JVM is plate form dependent to make Java programming plate form indepdent.  

Difference between Class and Interface

Java Programming   *Difference between Class and Interface                                                  Class 1.  The members of a class can be constant or variables.  2. The class definition can contain code for each of its methods. That is, the methods can be    abstract or non-abstract. 3. It can be instantiated by declaring objects. 4. It can use various access specifier like public,private or protected.                                                Interface     1. The members of an interface are always declared as constant, i.e. their values are final.  2. The methods in an interface are abstract in nature, i.e., there is no code associated with  ...

OPERATING SYSTEM

1.  The term 'operating system'   means: a)  the way a computer operator   works. b)  conversion of high level languages into machine   code. c)  a set of programs which controls computer   working. d)  none of the   above. 2.  What is the name of the operating system that reads and reacts in terms of actual   time: a)   Batch   system b) Time sharing system c) Real   time   system d) all of the above. 3.  The primary job of the operating system of a computer is   to: a)   command   resources b) manage   resources c) be   user friendly d) only one of the   above. 4.  Multiprogramming   systems: a) are easier to develop than single programming systems. b) execute more jobs in same time period. c) are used only on large   mainframe   computers. d) all of the above 5.  Which of the following is not an a...