Competitive coding problem

I want to print a series like
0 0 | 10 0 | 10 20 | -20 20 | -20 -20 | 30 -20 | 30 40 | -40 40 | -40 -40 | 50 -40 | …

Well, short of giving you the answer, try to figure out what it is doing. First of all, I’d think of this as two problems: the left number and the right number. Assuming we’re doing this with a for loop, how does each number relate to the index? Assuming you are starting at something like i = 0, how do you derive each number sequence from that. You are going to have a use a few little math tricks to do it - see if you can figure it out. If it helps, ignore the 0s at the end of the numbers - they are trivial.

3 Likes

Hi @ajinkyabawaskar, welcome on the forum.
Here is a tip: notice that you modify alternately the first and the second number of the couple. In none of the elements in the list, both numbers are modified at the same time. Also notice that the signs of the numbers are alternated and they follow a specific pattern. As @kevinSmith said, focusing on each number singularly helps to understand such pattern.

Also, can you provide us with the code that you tried to so far? You can use codepen.io or JSFiddle

1 Like

I know codepen isn’t meant for C code, but I am using it anyways as I haven’t discovered any C fiddles yet. Also, I know the code I shared has poor coding practices like no proper naming of variables and maybe it just sucks. I was supposed to solve this problem in half an hour without an IDE, if I did, I’d have been eligible to appear for an interview for a job that pays 18k$/year. I am not able to do anything, not getting the solution is bugging me, even though I have traced the pattern, I am not able to convert it to code even though I know it is just loops and conditionals. Sorry about the details. Here’s what I achieved so far - https://codepen.io/xajinkya/pen/jgabym - the first digit in pattern without minus sign

repl.it will let you create a C environment (they support many different languages)

1 Like

Thank you @ilenia
here you go https://repl.it/@ajinkyabawaskar/patterns?language=c

Hi @ajinkyabawaskar , I’d like to say few things.

First, I do hope this question has not been asked on the forum during those 30 minutes you mentioned because the idea behind these questions are to evaluate the candidate, not his/her network.

Secondly, you should mark as the solution one of the posts of the people that provided an answer to the question, not your own answer where you added the link to the online code.
If the idea was that to make it stick at the start of the page, you can edit your own post to provide the additional information.

Edit: removed pseudocode answer.

1 Like

@simonebogni We usually try not to blurt out answers.

And to Ajinkya, I didn’t notice that you were working in C. It is fairly easy to do that locally from the command line.

1 Like

Hey @simonebogni , No. It was a test conducted by largest software company of my country. They have their own OS, with only one window accessible, i.e. the exam console. And, I have been participating in programming contests for a while so I know it is not right to ask for answers here and there. So no, I didn’t ask for the solution while the exam was being conducted. Also, after I figured out the answer, I updated my code to the online editor so that if in future, someone is stuck at the same problem as mentioned, they can have a quick peek into what solution would look like.
I will take your points in consideration next time I post here. Thank you for taking time out and working on that pseudo code.