Problem 1: Balanced string

“Everything should be balanced and your life is too. You could spend a lot of time for work but please save time for your family”, said by a good father Toan. “And a string should be as well, I think ToannaoT is better than my name”, he went ahead. And he invented a “balanced string” that its characters are mirrored by its center. For example, abcba or abccba are balanced while abcbc and abccbc are not.

Give you a list of string, compute the sum of length of all balanced strings.

Note: it’s also called “palindrome” which reads the same backward or forward.

Sample input:

Sample output:

Problem 2: Phone number

Cuong has a locked iPhone and it always gives him a headache of contact and phone number mapping. For example, he saved his girlfriend’s number 0987654321 to his contact, but it always shows up 84-9876-54321 as it adds the country code automatically.

As a super programmer, he of course wants to fix this by a coding service to recognize each parts and join it himself. Do you want to solve it with Cuong?

Give you a list of phone numbers, each number in each line. A phone number has a format

PhoneNumber = [CountryCode][separator][LocalAreaCode][separator][Number]

With

  • separator is space or hyphen
  • CountryCode, LocalAreaCode, Number only contain the digits.

Submit the result = [Sum_of_CountryCode] * [Sum_of_LocalAreaCode] * [Sum_of_Number]

Sample input:

Sample output:

Problem 3: Love letter

Yesterday, The just found a love letter that his friend, Long, has writtent to his girldfriend. As a naughty boy, he want to meddle with this letter and change all the words to “ballanced” (palindrome) that Toan has invented. But to be safe, he should follow the rule and save his steps to make this letter recoveryable later.

  • He can reduce the value of a letter but cannot increase. For example, he can change b to a but cannot change a to b.
  • Once a letter has been changed to a, it can no longer to be changed.
  • Each step, he can reduce 1 letter with 1 unit. For example, changing c to a needs 2 steps.

So let find the minimum steps for him to change this letter to palindrome.

Sample input

Sample output

Problem 4: Girl attracting law by Fibonacci approach

After a long time in-door researched, Hung found a good approach to attract a girl and he named it“Girl attracting law by Fibonacci approach” that means the number of presents Hung gives to her is increased by Fibonacci sequence. But don’t stop, he extended to version 2 with some customisation by:

T(n+2) = T(n+1) * T(n+1) + T(n)

with T(n) is a number of presents at the nth dating.

Hung of course has a lot of money but wants to trust a girl, so he should give her 0 present at the first and 1 present at the second dating. And following his theory, 5 presents should be given in the 5th dating as explanation below:

1st number  = 0

2nd number = 1

3rd number = 12 + 0 = 1

4th number = 12 + 1 = 2

5th number = 22 + 1 = 5

Hung said that he has meet her 13 times and today he wants to know how many present he should give. Could you help Hung?

These problems are modified by some in Hackerrank

138 total views, no views today

Problem

After a long time in-door researched, Hung found a good approach to attract a girl and he named it “Girl attracting law by Fibonacci approach” that means the number of presents Hung gives to her is increased by Fibonacci sequence. But don’t stop, he extended to version 2 with some customisation by:

T(n+2) = T(n+1) * T(n+1) + T(n)

with T(n) is a number of presents at the nth dating.

Hung of course has a lot of money but wants to trust a girl, so he should give her 0 present at the first and 1 present at the second dating. And following his theory, 5 presents should be given in the 5th dating as explanation below:

1st number  = 0

2nd number = 1

3rd number = 12 + 0 = 1

4th number = 12 + 1 = 2

5th number = 22 + 1 = 5

Hung said that he has meet her 13 times and today he wants to know how many present he should give. Could you help Hung?

Solution

It seems very easy because the Fibonacci is a basic problem we usually do in learning programming. But, please aware that the number in this sequence increase too fast and need a wide range of positive number. In C# or Java, you could use BigInteger type.

 

103 total views, no views today

Following my Code Challenge problem Cute girl decoder, here is the solution:

Solution

It seems very easy to solve this problem by replacing the substring such as j by gi, dz by d, k by khong.

But, there is a trick at “With words, she usually use”, that means k should be replace by khong only if it’s a word that is normally separated by these characters: [space] , . ; ? !

You could find an implementation below in Objective-C, my thank to Nghia Luong for the code contribution.

It looks good but in my opinion, we could make it better with the open design.

Better design

Let see my implementation below as I think it could be better design to extend the implementation in the future. But please try to find some area we can improve 🙂

And the decoded message would be:

chan qua. em mun an nhieu ga quay cho doi gio duoc khong day? Anh co phai la nguoi iu cua em khong day, neu a la nguoi ieu cua em thi phai dua em di an ga quay chu, anh ma hem dua em di em gian a lien doa. The gio anh mun nhu the nao? ket qua ha? ok, neu ma anh khong dua iem di an ga ran thi nguoi dung den gap em lam gii nua, em khong yeu anh nua dau, iu gi ma co moi viec dua di an ga quay cung khong duoc thi yeu lam gi day, chan anh vagi dan ra ay. Ngay hom sau em noi gi a, em van noi la: chan qua. em mun an nhieu ga quay cho doi gio duoc khong day? Anh co phai la nguoi iu cua em khong day, neu a la nguoi ieu cua em thi phai dua em di an ga quay chu, anh ma hem dua em di em gian a lien doa. The gio anh mun nhu the nao? ket qua ha? ok, neu ma anh khong dua iem di an ga ran thi nguoi dung den gap em lam gii nua, em khong yeu anh nua dau, iu gi ma co moi viec dua di an ga quay cung khong duoc thi yeu lam gi day, chan anh vagi dan ra ay. Ngay hom sau nua em noi gi a, em noi day ne: chan qua. em mun an nhieu ga quay cho doi gio duoc khong day? Anh co phai la nguoi iu cua em khong day, neu a la nguoi ieu cua em thi phai dua em di an ga quay chu, anh ma hem dua em di em gian a lien doa. The gio anh mun nhu the nao? ket qua ha? ok, neu ma anh khong dua iem di an ga ran thi nguoi dung den gap em lam gii nua, em khong yeu anh nua dau, iu gi ma co moi viec dua di an ga quay cung khong duoc thi yeu lam gi day, chan anh vagi dan ra ay.

Go further

The idea of Skype plugin just came to make this problem more fun. But do you want to make it go live? I didn’t build it yet, but if you like this idea, I would like to code and share in the next post.

Do you want to see the new Skype plugin? Throw me at least 50 likes 🙂

140 total views, no views today

Long had a new girl friend, congrats to him. He feels very happy by chatting with her everyday although it’s not an easy work. As a 9x cute girl, she uses a lot of teenage chatting words and it usually takes Long more time to read and understand. And of course, it takes him a lot of his brain’s energy :).

To make it easy, Long plans to build a Skype plugin that can translate her sentences to the normal ones. He found that it’s so easy to replace the cute words by the following rules:

  • With words, she usually use
    • k for khong
    • ko for khong
    • ng for nguoi
    • n for nhieu
    • dc for duoc
    • hok for khong
    • ntn for nhu the nao
    • kq for ket qua
  • She also use these characters
    • j for gi
    • w for qu
    • f for ph
    • dz for d
    • z for d

So if she says “chan wa. em mun an n ga way cho doi jo dc hok dzay?“, it is “chan qua. em muon an nhieu ga quay cho doi gio duoc khong day?

Could you help Long to write this plugin. After that, please try to translate her recent message:

chan wa. em mun an n ga way cho doi jo dc hok dzay? Anh co phai la ng iu cua em k zay, neu a la ng ieu cua em thi fai dua em di an ga way chu, anh ma hem dua em di em jan a lien doa. The gio anh mun ntn? kq ha? ok, neu ma anh hok dua iem di an ga ran thi ng dung den gap em lam ji nua, em k yeu anh nua dau, iu j ma co moi viec dua di an ga way cung ko duoc thi yeu lam j dzay, chan anh vaj dan ra ay. Ngay hom sau em noi j a, em van noi la: chan wa. em mun an n ga way cho doi jo dc hok dzay? Anh co phai la ng iu cua em k zay, neu a la ng ieu cua em thi fai dua em di an ga way chu, anh ma hem dua em di em jan a lien doa. The gio anh mun ntn? kq ha? ok, neu ma anh hok dua iem di an ga ran thi ng dung den gap em lam ji nua, em k yeu anh nua dau, iu j ma co moi viec dua di an ga way cung ko duoc thi yeu lam j dzay, chan anh vaj dan ra ay. Ngay hom sau nua em noi j a, em noi dzay ne: chan wa. em mun an n ga way cho doi jo dc hok dzay? Anh co phai la ng iu cua em k zay, neu a la ng ieu cua em thi fai dua em di an ga way chu, anh ma hem dua em di em jan a lien doa. The gio anh mun ntn? kq ha? ok, neu ma anh hok dua iem di an ga ran thi ng dung den gap em lam ji nua, em k yeu anh nua dau, iu j ma co moi viec dua di an ga way cung ko duoc thi yeu lam j dzay, chan anh vaj dan ra ay.

The solution will be updated in 3 days.

63 total views, no views today

Do you remember my post about the interview question? It’s one of my problems in our Code Challenge that I would like to share in this post.

Code Challenge is one of our exciting activities last year where we can learn from each other by solving the problems in a contest. I also published the tool on Github for who wants to host this kind of event, please don’t hesitate to contact me if you want to use or need any support.

Purpose

It makes sense, everybody always needs to improve as every organisation needs to go forward. And learning is a good way to keep us improved and motivated. We have thought about the contest that allows every developers join to learn and measure their coding skill, and we held the Code Challenge last year that was involved by all our developers as contestants.

Joining the contest with our colleagues is very fun thing as we can learn from others and see how good we are right now. But it’s not easy for the host because there are various specific platforms or technologies in one organisation or teams, then the contest should target on a shared knowledge or skills in the whole team. To be fair, we should remove all the dependencies that relate to the language or platform uses and back to the basic of software development: programming.

Follow my experience, the ACM ICPC is very good format that focus on the programming but it seems still complex and has language limitation. Project Euler suits us better but it should have the time constraint to identify the winner.

Format

Our Code Challenge is mostly like the Project Euler format with time limitation as ACM ICPC. It was being held bi-weekly in 5 rounds, each round run from 13:00 to 17:00. In 4 hours, the contestants try to solve about 5 or 6 problems by following rules:

  • Solve problems in order. The contestant needs to solve a problem before taking the next one. So the problems should be arranged by their difficulties.
  • Result comparison. It doesn’t matter with the contestants’ solution as they can solve the problem by any programming language (or sometimes by non-programming as by Excel skill). A problem just shows the issue with a specific input data and compare the result with correct solution.
  • Multiple submissions. Contestant can try submit his solution many times as he wants to get the problem solved. The time spend for a problem is that the time he starts the contest to the first attempt of accepted solution.
  • Faster is better, careful is good. The contestants are ranked by these criteria in order:
    • Number of solved problems. The more number of solved problems are, the higher rank of contestant is.
    • Number of submission. If some contestants have the same number of solved problems, the number of submission should be counted. The contestant get higher rank by fewer attempts.
    • Time spend. The time spend should be the last criteria as its precision was counted to millisecond :). It is the total time spend of all solved problems.

Experiences

As our experience, there are some good things we could have from Code Challenge:

  • Fun. It could come from various sources such as a story in problem or the solution. In the 3rd round, we all surprised by a problem can be solved in 3 minutes as it normally takes at least 15 minutes for programming. But it’s so easy to calculate and submit the possible results manually. Yes, he’s so smart.
  • Share and learn. After the contest, we always sit together to share our different solutions of those problems, so we can learn from others and see what was the best way to solve them.
  • Improve. We of course had a good chance to learn from others or from practice during the contest preparation for getting improved.
  • Motivate. Beside the award for winner, there were some secondary awards such as the best newbie, first solved problem, last accepted attempt…etc for our guys trying to get.

And there are a lot of good things you would see as I think you should host at least one Code Challenge in your team.

Issues

Yes, there are various good things we had but in another hand, some minor issues came.

You could see that our format and tool were designed to target the purpose: removing the technology dependencies where contestant can solve the problem by any language (or by non-programming). But the issue is moved to the problem set as we should design them as free-language algorithm – it’s usually not easy. A lot of problems can be solved quickly by Java, C# but much slower in C++ by coding time but versa in running time.

Another issue is that somebody is very good at basic programming and algorithm but someone isn’t. I truly believe that a good developer should be good at algorithm but it seems not work today as the technology goes so far and developer can build the very good products with a normal algorithm background. He of course cannot be bad but doesn’t need to be very strong in algorithm to be a good developer as some years ago because the high-level technologies support us too much. For example, 10 years ago, every good developer needs to know what best sorting algorithm in his case in C++ but today a normal developer doesn’t care about this, just call Sort() method in C# and it would do in the best way. And of course, the results are mostly the same no matter he knows the Sort() method would run by any algorithm. It’s proved in our Code Challenge last year as there was only 1 winner for all rounds. He is very talent and strong in algorithm but isn’t a top guy in product development.

They are just the minor issues we would face in any contest that targets to wide range of developers. The problem content is the best way to resolve these issues but it usually takes much time to create a good problem like that. Contest is not fair but we should keep it as good as possible 🙂

I will share our problems that were used in our Code Challenge last year in some next posts and hope that you can find some interesting things.

And here is the system we have used: https://github.com/hiennvn/code-challenge. It may have some bugs because it just was the first MVP and one of my 24-coding-hours projects. But it worked fine for us last year.

Keep calm and love code.

Don’t want to wait for my next post? Try this problem:

A motor bike plate number is in the format [AB-CD EFG.HI]. Its value is defined by: AB x C + EFG x HI, if C is a letter, its value is its ASCII code. A plate number is called NICE one if its EFG contains its HI, then its value is doubled than normal. Give you a list of place numbers, find the biggest value.

For example: A plate 29-C1 320.12 has value 5783 (= 29*67+320*12)

What is the biggest value of

28-A1 493.68

83-Y3 453.83

17-Z7 439.48

29-C1 292.29

142 total views, no views today