Sie sind auf Seite 1von 14

Google Code Jam Practice Problems

A - Big City Skyline


You've just moved to the Big City so you could start work at the newest Google
office, Google BC, and the
one thing that interests you most is the beautiful skyline. You're sitting on a
hillside ten miles away, looking
at the big rectangular buildings that make up the city, and you think back to
your childhood.
When you were a child, you used to make toy cities out of rectangular blocks.
Each building could be made
from multiple blocks, and each block could be part of multiple buildings.
Looking at the skyline, you wonder:
what is the biggest possible block that could be part of the Big City's skyline?
Write a program that takes the description of the skyline as an input, and gives
the area of the maximumarea rectangle as output. This program should take
less than 4 minutes to run on a 2GHz computer with
512MB of RAM, even for the biggest input size we specify.
Input:
The city is made out of rectangular buildings, all next to each other. The input
will consist of an integer N,
the number of buildings, followed by a series of N number pairs wi hi,
indicating the width and height of each
building in order from left to right. The buildings' heights will be less than
100,000,000, and their widths will
be less than 1000.
Easy: 0 < N < 1,000
Hard: 0 < N < 10,000,000
Output:
A single number: the area of the largest possible block. (Here’s the beautiful
skyline of the Big City.)
Sample Input Sample Output Explanation
5
5 7 1 20 3 5 6 3 2 10
51 The biggest block is shaded black
in the figure above.
Google Code Jam Practice Problems
© Google Inc. - September 2007

p. 1

B - Universal Search
You've just started your job at Google Moon, and you've been assigned to the
universal search
project: that is, of course, the functionality that lets people search for anything
in the
universe. Your starter project seems simple: find a flight path for the GCrawler
(Galaxy Crawler)
that will gather stock prices from all of the galactic stock exchanges, traveling
between planets
using Hyperspace Jumpgates.
The known universe contains k stock exchanges and p inhabited planets. Each
planet deals with
one stock exchange, and every planet has a record of the stock prices for its
exchange. Given a
list of stock exchanges, their planets, and planet<->planet jumpgate
connections, find a path for
the GCrawler that contains the smallest number of jumpgates, but still passes
through at least
one planet from each stock exchange before returning home.
Input:
The input will start with p, the number of planets. Next will follow p lines of the
form:
planet_name stock_exchange connection1 connection2 ... connectionN
If planet A connects to planet B, then planet B also connects to planet A. Planet
and stock
exchange names will contain only alphabetic characters [A-Za-z]. One of the
planets will be
named GoogleMoon; that is the start and end location for the GCrawler.
Easy: 0 < p < 50, 0 < k < 5.
Hard: 0 < p < 500, 0 < k < 16.
Output:
A series of planet names, separated by spaces, describing the path the
GCrawler should take.
Sample Input
5
GoogleMoon TheInternet Mars Earth
Mars EarthStocks GoogleMoon AlphaCentauriII AlphaCetiV
Earth EarthStocks GoogleMoon
AlphaCentauriII AlphaStocks Mars AlphaCetiV
AlphaCetiV AlphaStocks Mars AlphaCentauriII
Sample Output
GoogleMoon Mars AlphaCetiV Mars GoogleMoon
Explanation
The GCrawler has three stock exchanges to visit: TheInternet,
EarthStocks and AlphaStocks. It starts on GoogleMoon, which
deals with TheInternet; it then travels to Mars, which trades in
EarthStocks, then to AlphaCetiV, which deals in AlphaStocks.
Now it needs to get back home; the shortest route goes
through Mars.

3:Problem Statement: Consider a permutation of numbers from 1 to N written


on a paper. Let’s denote the product of its element as ‘prod’ and the sum of its
elements as ‘sum’. Given a positive integer N, your task is to determine
whether ‘prod’ is divisible by ‘sum’ or not.

Input Format: First input will be an integer T. It depicts a number of test cases.
Followed by value f0r each test cases. Each test case will contain an integer N
(1<= N <=10^9). It is nothing but the length of the permutation.

Output Format: For each test case, print “YEAH” if ‘prod’ is divisible by ‘sum’,
otherwise print “NAH”.

Que::

3. Maximum Profit by buying and selling stocks


This competitive coding question is asked in Goldman Sachs.

Problem Statement: Suppose you have given the stocks prices for respective
days like (100, 180, 260, 310, 40, 535, 695). The stock price for the 1st day is
100, 2nd day it is 180 and so on. Write a Python program to determine what
days the user should bye and sell the stocks to get the maximum profit.

In the above case, in the following scenarios user will get maximum profit.

Buy stock on 1st day (100)


Sell stock on 4th day (310)
Buy stock on 5th day (100)
Sell stock on 7th day (695)
Algorithm steps:

Find the local minima (buying stock)


Find local maxima (selling stock)
Repeat until all days are covered.
Python Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
liStocks = [100, 180, 260, 310, 40, 535, 695]

#find local minima


def findMin(liStocks):
for i, val in enumerate(liStocks[:-1]):
if val < liStocks[i+1]:
return i, val
return -1, -1

#find local maxima


def findMax(liStocks):
for i, val in enumerate(liStocks[:-1]):
if val > liStocks[i+1]:
return i, val
return i+1, liStocks[-1]

def byeSellStock():
index=0
while index < len(liStocks):
i, val = findMin(liStocks[index:])
if i > -1:
index=i+index
print("bye stock on day ", index+1, val)
else:
break

i, val = findMax(liStocks[index:])
index=i+index
print("sell stock on day ", index+1, val)

if __name__ == "__main__":
byeSellStock()
Output:

buy stock on day 1 100


sell stock on day 4 310
bye stock on day 5 40
sell stock on day 7 695

5::
Problem Statement: Secure My Conversation by Encryption and Decryption

Person A and B use an encryption-based system for their conversation.

Each conversation message is encrypted from the source and decrypted in the
destination using a shared private positive number key known to each other.

The algorithm is illustrated with an example.


Input format with explanation:

Operation (1 for Encryption and 2 for Decryption)


Input message
Input private key
Output format with explanation:

Output message
Example 1:

Input:

1
Open
123
Output:

Oppeeen
Here, the input message characters are duplicated based on each digit in the
key.

Example 2:

Input:

2
Oppeeen
123
Output:

Open
Here, the input message characters are compressed based on each digit in the
key.

The conversation message and the private key need NOT be in equal length
and the encoding/decoding takes place till the end is reached either
conversation message or private key while retaining the rest of the
conversation message.

Programming Solution in Python:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/python3

import math
import os
import random
import re
import sys
# Complete the secureChannel function below.
def secureChannel(operation, message, key):
if len(message)==0 or len(key)==0:
return "-1"

strRet=""

if operation==1:
index=0
for keyVal in key:
nKeyVal=int(keyVal)
if index>=len(message):
return strRet
strRet=strRet+message[index]*nKeyVal
index=index+1
if index < len(message):
strRet=strRet+message[index:]

elif operation==2:
index=0
for keyVal in key:
nKeyVal=int(keyVal)
if index>=len(message):
return strRet
for i in range(0,nKeyVal):
if (index+i)>=len(message):
return "-1"
if message[index] != message[index+i]:
return "-1"
strRet=strRet+message[index]
index=index+nKeyVal
if index < len(message):
strRet=strRet+message[index:]

else:
return "-1"

return strRet

if __name__ == '__main__':
print(secureChannel(1, 'Open', '123'))
print(secureChannel(2, 'Oppeeen', '123'))
Output:

Oppeeen
Open

Das könnte Ihnen auch gefallen