Following my problem in the first post of Code Challenge series, it’s a solution.

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

Solution

It seems easy where we can compute the value of a specific plate number and find a biggest value afterward.

But, please aware that there is a trick at if C is a letter, its value is its ASCII code. So C can be a digit or letter. Only if it is not a digit, we need to get its value by ASCII code.

Here is my solution in C#

Here is the solution in Objective-C, credit to Nghia Luong to share.

No code

Yes, at least we can solve it by Excel as can compute a plate number value by this function

Note: 

My full input is in attachment, not just a simple input above as it’s easy to solve manually.

[wpba-attachment-list]

80 total views, no views today

Some days ago, after I published my interview question, my colleague asked me about having the problems weekly and solving them together. And instead of theory problem, they should be the real problems that we would face in the daily work. And today we did the first one.

One of the daily problems is making the mock test data as we want to have a lot of data and insert them to a test DB. I’m writing a library that can help my team to generate a random data for test and hope that I can complete and share with you in some next days. But I need to solve the first problem while writing it:

When inserting the data to DB, we need to put them in a correct order to satisfied its constraints. Let’s focus on the reference (foreign key) constraints to make it simple enough. For example, if there are 2 tables Product, Category in the DB, one product should belong to 1 category, so the statements should be:

If we change these statements to:

it doesn’t work because the the CategoryId = 1 isn’t existing while the first statement runs. So the problem should be: Giving a clean DB, so we know its schema (and of course, we know all tables’ references), and some INSERT statements, which should be the correct order of these statements? Or which the order of tables should be inserted? For example, it should be (Category, Product) in above sample.

We had a discussion on My Khe (Danang) beach to solve this problem this time, it was very interesting. Finally we had some solutions, and one of them has came up after answering some questions:

1. What are the first tables?

Of course, they are the tables that have no reference to any others. Let call this table set A.

2. What are the next tables?

They are the tables that only refers to the tables in A or itself. Let call this table set B.

3. What are the next tables?

They are the tables that only refers to the tables in A or B or itself. Let call this table set C.

4. The #2 and #3 questions are the same?

Yes.

5. The table set A in #2 answer and (A or B) in #3 answer have the same meaning?

Yes.

So repeat to answer #2 question, we should have a natural approach. The algorithm could be described as: Start with the table that don’t have any reference and add them to table set A, try to find the next tables that their references only belong to A and add them to A in a loop until all table are in A.

And the implementation in C# could be:

I used DatabaseSchemaReader to get the DB’s schema in this C# code. Of course, you can find another appropriate library in your platform or language as it seems a common problem.

Thank you my friend for giving me the solution and hope that I can get the test data generator done and share with you soon.

Solving a problem, get it done by code and publish it on the blog – all on the beach, it’s such a great experience.

Writing on the beach

Note: You could see that it’s not just a DB problem itself, and it can be solved by graph or natural algorithm. The DB problem here is just reasonable for having references between the nodes (we can consider that a table is a node, and its references to other tables are the connections in a graph). So you could see the importance of the basic data structure and algorithm to solve any real problem.

I updated the solution and code by the comment of Nguyen Phuc Dat below in case of self-referenced. Dat, thank you guy.

150 total views, no views today

The most important job is recruiting.

Yes, it’s totally true. I have spent about a quarter of my time for finding the good guys and got both success and failure in seeing how good a candidate is. Then I have tried to create a question and used it for a year in all my interviews with candidates of developer position.

The interview question is:

Writing continuously the non-negative numbers by skipping the number which is built by only one digit (11, 22, 33… 111, 222, …), we have a sequence:

0123456789101213…19202123…

You can see the 10th digit is 1. So let’s say 10th 1. Compute:

10th + 100th + 1.000th + …

My question is “What is your approach to solve this problem? Don’t need to give the final result”.

It’s so easy? Not sure – as it’s a kind of a problem that its hard increases fast by the data limitation. Could you find 100th? 1000th? 100.000.000.000th? Yes, it’s much more different.

OK, let see some solutions that are described below:

Solution 1: Natural thinking

It seems we can build up and store this digit sequence in a string. And it’s so easy to get the digit by the index then. The code should be

But how about the 100.000.000.000th?

Solution 2: Optimisation

We can consider to stop the loop as soon as possible. For example, to generate a string with length 100.000.000.000, the maximum value of number in the loop should be less than 10.000.000.000 (why? could it be a smaller number?).

But it’s still too large. How many memory it takes to store 10.000.000.000 characters? 10.000.000.000 bytes > 9GB. But wait, why do we need to store whole the string? How many meaning digit? They are just 10th, 100th, 1.000th.. digits. So just count to get the meaning digit and skip storing others. The code should look like

This way can reduce the memory use to constant and nearly 0. But 10.000.000.000 seems too big for reaching in loop.

Solution 3: Math approach

Let’s see another approach by math

  • How many digits do we have by writing a sequence from 0 to 9? 10
  • How many digits do we have by writing a sequence from 10 to 99? 162 = (99 – 10 + 1 – 9) * 2. Skipping 9 numbers: 11, 22, …
  • How many digits do we have by writing a sequence from 100 to 999? 2673 = (999 – 100 + 1 – 9) * 3. Skipping 9 numbers: 111, 222, …

OK, so what 4.321th digit? 4321 – 10 – 162 – 2673 = 1476 > 0, this digit belongs to a number that falls in (1.000, 9.999) which has 4 digits. So its index from 1.000 = 1476 / 4 = 369. Then the finding digit should be 1 (in 1.369).

It’s not much easy but not difficult to implement in code and the process time is constant now.

It’s just a problem itself but how people approach or solve it can tell much about them.

After about one hundred interviews, I had a lot of data and categorise the candidates by the common feedback questions:

  1. I cannot understand this question. I get no information because some of developers are super in building software by tools but have never faced this kind of problem. Just explain or give them another question.
  2. I cannot solve this. Say goodbye to him, at least easy giving up is not my style or he should give me #1 feedback.
  3. I think there is a rule here. 10th is 1 so I guess 100th, 1.000th, 10.000th.. are 1 as well. Life is not easy man. The interview question is for testing the logical not tricky thought.
  4. What is its input? How to input data, from console? Why 10th is 1, it is 9?… I usually don’t waste my time for answering these questions. He just focuses on the minor things to forget real problem.
  5. We can generate this string and get the digit by index after that. Yes, it’s the first solution, right? He looks like a normal developer. I usually go ahead with him by some questions depending on how he structure the program. “How many functions or methods you have?”, “How do we check a number that is built by 1 digit?” etc. And of course, some suggestion to optimise the solution, “What should you choose, optimise memory or speed?”, “How many meaning digits we have?” etc, and the story begins.
  6. Ah yes, we should not maintain the whole string, we can remove the unnescessary parts. Sure, the second solution. He seems a good developer.
  7. Yeah, we remove 9 numbers in 2-digit numbers that equals to 18 digits. We also remove 9 numbers in 3-digit numbers that equals to 27 digits. And the actual position of 1.0000 should be minused… He is a must-have guy at least in the talent aspect.

It just a common test for his IQ, but the specific code should shows his knowledge and skills in a specific language or platform. For example, good C# developer should use StringBuilder to build the sequence instead of using string. C++ developer should check the number that is built by 1 digit by operations instead of by converting it to string. Putting the condition like if (sequence.Length > 10.000.000.000) in the loop is not the good way in code in any language.

The guy who can show off the solution 3 in the first time – he is really intelligent or he has solved a thousand problems like that, maybe a code challenger, also a right man we would hire.

The guy who cannot pose the best solution at the first time but can come to the solution 3 after the advises has potential to become a great developer.

The guy who cannot come to the optimised solution after the advises just be a normal developer – not a smart guy. I have never hired him even though he has a lot of experience or certificated.

In my opinion, Java, .NET or what common technologies is so easy for a talent guy learns in a short time but technology-master cannot go far with a week algorithm coding background.

So far, I think it’s a good question to see the developer’s IQ that I used in a long time. Now I’m creating another :).

126 total views, no views today