Вы находитесь на странице: 1из 19

Facebook Hacker Cup 2013: Beautiful Strings Solution

The problem statement is given below:

When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no
programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to
discover the most beautiful string in the world.

Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same
beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the
beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)

You're a student writing a report on the youth of this famous hacker. You found the string that Johnny
considered most beautiful. What is the maximum possible beauty of this string?

The input file consists of a single integer m followed by m lines.

Your output should consist of, for each test case, a line containing the string "Case #x: y" where x is the
case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum
beauty for that test case.

5 m 50
2 length of s 500

Sample Input

Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves

Sample Output

Case #1: 152

Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646
Here goes my solution for the Beautiful Strings problem:

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;

* @author VIK
public class Problem1 {
private static ArrayList < Integer > integers;
private static File f=new File("Solution1.txt");
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("beautiful_stringstxt.txt");
LineNumberReader lnr = new LineNumberReader(fr);
int t = Integer.parseInt(lnr.readLine().trim());
FileWriter fw=new FileWriter(f);
for (int i = 0; i < t; i++) {
int sum= getMax(lnr.readLine().trim());
if ( i < t-1 )
fw.write ( "Case #" + ( i+1 ) + ": " + sum+ "\n" );
fw.write( "Case #" + (i+1) + ": " + sum );

private static int getMax ( String string ) throws Exception {
integers = new ArrayList < Integer > ();
for ( int i = 65, j = 97; i <= 90 && j <= 123 ; i++, j++ ) {
String s = string.replaceAll ( "[^" + (char) i + (char) j + "]", "" );
int sum = 0;
for ( int i = integers.size() - 1, j = 26; i >= 0 && j >= 1; --i, --j) {
sum += integers.get(i) * j;
return sum;

Identify the logic behind the series

6 28 66 120 190 276....

The numbers in the series should be used to create a Pyramid.

The base of the Pyramid will be the widest and will start
converging towards the top where there will only be one
element. Each successive layer will have one number less
than that on the layer below it. The width of the Pyramid is
specified by an input parameter N. In other words there will be
N numbers on the bottom layer of the pyramid.

The Pyramid construction rules are as follows

1. First number in the series should be at the top of the
2. Last N number of the series should be on the bottom-most
layer of the Pyramid, with Nth number being the right-most
number of this layer.
3. Numbers less than 5-digits must be padded with zeroes to
maintain the sanctity of a Pyramid when printed. Have a look at
the examples below to get a pictorial understanding of what
this rule actually means.

If input is 2, output will be

00028 00066

If input is 3, output will be

00028 00066
00120 00190 00276

Formal input and output specifications are stated below

Input Format:

First line of input will contain number N that corresponds to the

width of the bottom-most layer of the Pyramid

Output Format:
The Pyramid constructed out of numbers in the series as per
stated construction rules

1. 0 < N <= 14

Sample Input and Output

SNo. Input Output
1 2

2 3

Pseudo Code:
The series is subset of Hexagonal Number generated using
formula: n * ( 2 * n -1 )

If n is only even number then you will get the series as 6, 28,
66, 120, 190, 276, 378, 496, 630, 780, 946, 1128, 1326, 1540
and so on.

1. for i = 0 to i < N; i++

a. print blank space for (N- i - 1) times
for j = 0 to j <= i ; j++
a. n = ( i + j +1 ) * 2
b. num = n * ( 2 * n - 1 )
c. if number of digit in num < 5
print 0 for (number of digit in num - 5) times
d. print num
e. if ( j < i )
print space


Walter uses multiple applications at his workplace. He was assigned

a unique password of variable lengths for each application
(maximum length being 10). The only common factor among them
was that all the passwords were a combination of 10 distinct
characters. Walter created an algorithm to encrypt these passwords
and wrote the codes on his desk to avoid a situation in which he
would not be able to recollect a password.

Given as an input, is an encoded password and a list of 10 unique

characters. Write a program that obtains the password by reverting
the algorithm used by Walter.

Algorithm used by Walter to encode his password is as follows:

1. Walter observes all his passwords and lists down every

distinct character. He assigns an index value to each character
from 0 to 9. (Note: This list will be provided as an input to the
program). This list MUST contain 10 DISTINCT characters.
2. He then replaces every character in the password with the
corresponding index value to convert the password to a 10 digit
numeric value.
3. The leftmost digit in this number sequence is noted; let's
say 'A'. Now, starting from the leftmost position, every digit in
the number is added to the digit on its right. The last digit in the
sequence is added to 'A'.
4. Step 3 provides new sequences of numbers. He then scans
through each of the summations and if any of the summation
results in a value greater than 9, it is subtracted by 10, and its
position is noted (Say B).
5. He then lists every digit from 0 to 9 separated by '|'
character and prepends each digit with its position in the
sequence as noted in 'B'. The resulting sequence after this step
is saved (Say C).
6. The encoded password would therefore be
< value_of_C >||< contents_of_array_B > < Value_of_A >

Decrypt the encrypted password that has been created by Walter

using the above algorithm to compute the actual password!

Print -1 if there is any error in the inputs.

Input Format:
1. First line contains the encoded password as it should be
according to Walter's algorithm
2. Second line contains 10 unique characters

Output Format:

Obtained password by reverting the algorithm. Print -1 if there is any

error in the inputs.
1. Second line of input MUST contain 10 DISTINCT
2. Encoded password should be in format
< value_of_C >|| < contents_of_array_B >

Sample Input and Output

SNo. Input Output

1 0|1|2|43|14|5|6|7|308|29||0149

2 0|1|2|43|14|5|6|7|308|29||0149

Explanation of Test Case 1:

NOTE: - The following are the steps used to encrypt the password.
Your task is to reverse the logic and decrypt the encrypted string
provided as input, and get back the clear-text password.
1. Consider input characters and its position

Input character list:*Acf$Zd&T@

Position :0 1 2 3 4 5 6 7 8 9

(Note: The character list will not contain more than 10
characters and each character must be unique)
3. Now replace the characters in the password with the
position value of the respective character as defined in step 1, to
convert the password into a numeric value.

Password :@@Z$$
Numeric value:9 9 5 4 4

(Note: Remember leftmost value (Say A) = 9)
5. Starting from the left most position, add each digit to the
digit on its right. Add the last digit with the first digit in the

Numeric value of password:9 9 5 4 4

Addition : 18 14 9 8 13

7. For each addition result equal to or greater than 10,
subtract it by 10 and note its position.

Additive step output :18 14 9 8 13

Selective subtraction:8 4 9 8 3
Numeric value :0 1 4

(Note: Remember the selective subtraction step positions
obtained (Say B) = 014)
9. The password will follow the following format


10. Now, each number in this format is pre-appended with the

positions of the occurrence of the respective number in the
output obtained in selective subtraction step of Step 4.

Encrypted password:0|1|2|43|14|5|6|7|038|29

(Note: Say C = 0|1|2|43|14|5|6|7|038|29)
12. The final encrypted password will be of the following
format to account for the calculations performed in generating
the encrypted password

Encrypted password (format):< C >||< B >< A >

Encrypted password :0|1|2|43|14|5|6|7|308|29||0149
Explanation of Test Case 2:

Second line of input contains 9 characters only hence prints -1

1. Tell me about yourself.

Possible answer 1:

I am an extrovert who interacts well with people. I like to set goals for myself and accomplish them. I
am very persistent. These qualities have got me particularly well noticed during my college years. I
have had a good academic record so far.

Possible questions from your answer:

Q1. What goals have you set for yourself?

Q2. Can you give me an example where you have shown persistence?
Q3. Can you give an example where your interpersonal skills got you noticed in the

Possible answer 2:

I am a simple guy who believes that hard work is key to success. Since childhood I liked computer
thats why I opted Computer Science. I started programming when I was in class eleven. In college I
started participating in various programming contests. I have won several contests with my friends. I
spend my spare time in surfing internet, programming and blogging.

Possible questions from your answer:

Q1. What is your favourite programming language?

Q1. Rate yourself on a scale of 10 in your favourite programming language.
Q3. Name a few programming contests you won.
Q4. What do you explore on internet?
Q5. Name 5 websites, you visit most.

Possible Answer 3:

I am a fun loving person, who believes in living life as it comes. I am very fond of classical music and
have acquired a professional degree in this also. Playing online chess is my favourite pastime. I
believe Internet is the future of human civilization and would love to see myself as a successful
Marketing Manager.

Possible questions from your answer:

Q1. Tell me the name of three international chess players?

Q2. What is castling in chess?
Q3. Which websites do you visit most?
Q4. Why do you want to be a Marketing Manager?
Q5. Tell me 5 traits of a good marketing Manager.

Avoid such kind of answers:

I am Abc, currently pursuing B.Tech from Xyz University. I have scored 95% in class ten and 97 % in
class 12. I am currently working at XYZ limited as a developer.

Such kind of answers can ruin your first impression. All these details are mentioned
in your CV and there is no point on reciting same things again. Also avoid using abbreviations and
take care of grammar.

2. Have you brought your resume?

Yes. (Be prepared with two or three extra copies. Do not offer them unless you are asked for one.)

3. What are your strengths?

* Always back up your Qualities / Strengths with Examples even without being
asked to.

* With the help of stories & examples stick into the interviews mind , prove to them
you have ability you are claiming.

Possible answer:

I am hard working. As I said earlier, I set goals for myself and accomplish them. Whatever be the
condition I never ever give up because I think, where there is will, there is at least a way

Possible answer 3:

My greatest strength includes my analytic approach, my strong logic and my patience.

4. What are your weaknesses?

* Always backup your weakness with an example that how you are working to
overcome it.

Possible answer 1:

In the past, I had some trouble sharing responsibilities with others. I felt I could do things better and
faster myself. This sometimes backfired because I would end up with more work than I could handle
and quality of my work suffers.(back up with Example).But I have learnt from my mistakes and am
consciously trying to be a team leader.

Possible Answer 2:

When I do my task then sometimes I stick to it continuously such that I forget what is going on in
surrounding. But I am working on it and trying to avoid doing same thing continuously for longer
Possible answer 3:

My greatest strength includes my analytic approach, my strong logic and my patience.

5. Why should we hire you?

* Back up your answer with previous project / experience where you displayed excellence.

Possible answer 1:

I am very optimistic and hard working. Whatever be the condition I never ever give up. Others might
be more intelligent than me but if they are not hard working then whats the use of their talent. I
sincerely believe Hard work beats talent if talent does not work hard.

Possible answer 2:

Because I sincerely believe that I am well suited for this job. I realize there are other college students
who have ability to do this job but it will be difficult to find someone with the degree of work ethics I
have (example). I also bring an additional quality that makes me very best person for the job, my thirst
for excellence. Not just striving excellence, but putting every part of myself into achieving it.

Possible answer 3:

Since you have now evaluated me on all your benchmarks, I believe you would know better if I should
be selected for the profile or not. As from my side, my job is my passion and thats a one word
summary of my candidature.

6. If not selected today, what will be your future course

of actions?
* This is the second check point for your practical instincts. Here the interviewer is analyzing your
approach toward failures.

Possible answer:

I believe in Learning from my mistakes. Likewise, if I am not selected today, I will keep trying until I
get the job because I think, where there is will, there is at least a way. I will kindly request you to
tell me my shortcomings so that I can go back, work on those and sit again in front of you (obviously
if rules allow) to turn my failure into success.

7. What is more important to you money or work?

Possible answer:

Money and work both are like siblings/money and work both are important to me. But I also believe
when you work hard, money will flow to you. So work has a slighter greater edge than money.

8. Where do you see yourself five years from now?

* Here, the interviewer wants to check your instincts. He wants to know whether you
are a practical person or a day dreamer. This question is to check your planning
skills. Avoid giving answers like CEO of this organization or HOD of a department.
If you are an executive, Manager is the maximum you can attain in five years.

Possible answer:

Although it is difficult to predict things far in future, I know the directions I want to develop towards.
Within five years, I would like to become the very best (Talk about the position you would be joining
in as) your company has. My goal is to become an expert that others can rely upon. In making the
most of the position (Position of interest for you), my hope is that I will be fully prepared to take on
any challenges with greater responsibilities that might be presented in the long term.

9. Is there anything you would like to ask?

* This is the second level of your professionalism test. Many of you may consider this as the
simplest of all questions in an interview.

But to your surprise, this is the most important tool for an interviewer to verify your candidature so
far. He wants to know whether your answers are a result of overnight preparation or you actually
own that sense of professionalism. Avoid asking things like How is the incentive structure

Possible questions that can be asked:

Q1. Can you please tell me how I fared compared to the other candidates that you have

Q2. What work will be assigned to me?

Q3. What will be my career path in the company?

10. Why do you want to join this company?

*Here you need to explain why the company best fits as per your qualifications. Why the company
is ideal for your future. Why you are ideal for the company. You can praise the company by telling
them their achievement, work environment etc. You need to portray positive traits of the company.
All these things can be found in the company website.
Verbal Ability Test

In Verbal Ability Test, you need to write an email according to a given scenario
using the phrases given.
How to qualify Verbal Ability Test?
Use all phrases given in the outline. If you do not know the proper use of any
phrase then use that phrase in any simple sentence. Do not worry about meaning of
that sentence but do not make arbitrary sentences using all phrases.
Use proper salutation :
o Dear sir
o Dear Mr. Verma (if Verma is the surname of the person you are writing
email to)
o Dear Vikash (if Vikash is the first name of the person you are writing
email to)
Number of words must be greater than 50 as given in the instruction. Keep you
email between 70 to 80 words, it is considered ideal. Keep it simple and avoid using
long sentences.
Avoid any spelling mistakes. Proofread your email for any spelling mistakes. If
you do not remember spelling of any word then don't use that word.
Use proper form of leave-taking :
o Regards
o Thanks
o Thanks and regards
Sign the email with your name as given in the question. Do not worry, they
always give name in the question itself.


Atul does lots of online transactions and has accounts in

multiple banks, but to remember his passwords he created his
own encryption technique and wrote down the enciphered
passwords in a notepad. But he finds it very time consuming to
decipher those passwords so he has asked for your help to
develop a program to do the task quickly.

The approach he takes to encipher is,

1. First he writes down his password in a square matrix
P of dimension N, sequentially from the first element (see
example for better explanation). He chooses N such that
N^2 is the least number above the length of the password.
He then starts filling the matrix row-wise from top to
bottom. If any matrix element remains blank, he fills all
those blanks with the last element e.g. if the last element
filled is z then he fills all other remaining elements in the
matrix by z .
2. Then he creates new matrix A of the same
dimensions such that every A(i,j) = P(j,i). Next he assigns
the value of the alphabet in the password to that
corresponding element in the matrix. So 'a' is substituted
by 1, 'b' by 2, so on. Alphabets are only lower case. Also
passwords contain characters from a to z only.
3. Then he finds another matrix B such that, the
solution to the following equation gives a matrix with all
the elements of principal diagonal as 1 and rest as 0,
which is also called the eMat( encrypted matrix ) .

Your are provided with the eMat (encrypted matrix) or the

matrix B, your task is to find all possible passwords and print
Let the password be "passwords" as it has 9 characters , the
most appropriate matrix will be a 3X3 matrix, hence the matrix
will be
After Step 1:
After step 2:

After step 3:
The eMat for the given matrix will be
-0.78378378378 0.18918918919 0.70270270270
-0.11850311850 0.07900207900 0.09563409563
0.87733887734 -0.25155925156 -0.72557172557

Input Format:
1. First line contains integer N, which is the dimension
of square matrix eMat
2. Next N lines contain N space separated values (11-
digit precision after decimal point) representing the eMat.

Output Format:

In a single line print the password, if multiple password values

are possible print them separated by space, sorted by
ascending order of their lengths.

1. When eMat is converted to Matrix A, round off the

numbers in the matrix A to its nearest integer value to
recover the alphabets in the password

Sample Input and Output

SNo. Input Output

-0.78378378378 0.18918918919
1 -0.11850311850 0.07900207900 passwords
0.87733887734 -0.25155925156 -

2 0.06666666667 -0.06666666667 pas pass
-0.00350877193 0.05614035088