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

Concept of OOPS

Java Programming This are the basic concept of OOPS. 1. Object 2. Class 3. Data abstraction 4. Data encapsulation 5. Inheritance 6. Polymorphism 7. Dynamic binding Java Classes/Objects Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has  attributes , such as weight and color, and  methods , such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects. Create a Class To create a class, use the keyword  Myclass.java Create an Object In Java, an object is created from a class. We have already created the class named  MyClass , so now we can use this to create objects. To create an object of  MyClass , specify the class name, followed by the object name, and use the keyword  new : Example=> public class Myclass{ int x=10;...

DBMS LEARNING SHEET

                                                            Types of DATA Structured data are facts concerning objects and events. The most important structured data are numeric, character, and dates. Structured data are stored in tabular form. Unstructured data are multimedia data such as documents, photographs, maps, images, sound, and video clips. Unstructured data are most commonly found on Web servers and Web-enabled databases. Database A database is a collection of data that is organised in such a way that it provides efficient retrieval of desired information. This collected data could be in any format like printed, audio, electronic and graphic. Consider an example of an address book, it is also a database that provides information of personal contacts of persons. DBMS A Database Mana...
Java Programming