3
So glad I found this cool site. Wanted to try sharing a coding challenge a friend gave me, an interview question. Usually it is written in java, but you can use whatever language you want, just have fun!
Challenge: write efficiently and securely a program that creates an array of n threads that communicate with each other, where each thread, for simplicity, represents a number between 1,...,100. Oh, and it has to be randomly initialized.
Here comes the fun part: each thread looks at its neighbours(i-1,i+1) and:
If its number is smaller than the number of both of his neighbours(i-1,i+1), it increments itself.
If its number is larger than the numbers of both of his neighbours, it decrements itself.
If its number is equal or between the numbers of its neighbours, it keeps the number the same.
So far not fun, right? So let's also make it a circular array! (the first element in the array and the last element in the array are neighbours) the tricky parts are: each thread should not change its number before its neighbors finished checking it.
The program performs m rounds of checks.
Input: For program logic, you can take the input from the user, or define it, doesn't matter.
Output: first line should print the starting state of the array. Second line should print all of the numbers in the array after each thread did one check. Third line should print the numbers in all of the array after each thread did a second test(second round) and so on.
Might be quite tricky in the beginning, but have fun with it! The winner will be the one who can write the shortest code, and in case of a tie, cool algorithmic tricks or functional utilization of the java language will be awarded extra points!
First post here, will try to post more cool stuff for you to enjoy! Really like this site.
I posted it a while ago, but forgot my credentials while being abroad so I couldn't log in to the site and fix the previous post. Hoping that this one is according to the standards of the site and more than that, I really hope you'll enjoy this cool coding gamexercise.
Good luck to all and most importantly - enjoy!
As kevin well-said: Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
2Although a good challenge, I have the feeling this can be done without threads? All integers in the array check their neighbors, and increment/decrements/stay the same based on that, and continue doing so for
m
iterations. So if I understand correctly we get an inputn
(array-size) andm
(amount of iterations)? – Kevin Cruijssen – 2019-05-02T11:16:22.893it can be done without threads @KevinCruijssen, but using threads is more fun :) – Puzzle – 2019-05-02T11:22:56.453
1@Puzzle Well, not all languages have threads, hence the question. Also, if this is code-golf, doing it without threads would be shorter in Java as well. But after making an answer in 05AB1E I will try to make an answer in Java both with an without threads. :) – Kevin Cruijssen – 2019-05-02T11:24:50.907
1As for the random number, is it including or excluding
1
and100
? So is the range[1 ... 100]
,[2 ... 100]
,[1 ... 99]
, or[2 ... 99]
? It states in between, which would mean[2, 99]
. Or alternatively, are all four options allowed (and perhaps[0 ... 99]
/[0 ... 100]
as well? Just trying to save some bytes here, haha ;p – Kevin Cruijssen – 2019-05-02T12:30:36.1673Please define efficiently and securely. – Adám – 2019-05-02T12:38:38.583
Is it just me, or does this read like a homework problem, especially with the arbitrary addition of threads to the mix? – Xcali – 2019-05-02T14:21:22.337
@Adám: for instance, when you compare threads, you need to allow the thread check with its neighbors before going to the next random number and so on, you need to be aware not causing data inconsistency – Puzzle – 2019-05-02T15:58:55.287
i am not sure i understood @KevinCruijssen it is a cyclic, meaning the first and the last elements are neighbors. if you meant regarding the ranges, then it is including 1 and 100 – Puzzle – 2019-05-02T16:00:38.760
@Puzzle Yes, I indeed meant the ranges we get random values from. Ok, so
[1 ... 100]
. Will update my Java answer at the same time that I'll add the multi-threading program. – Kevin Cruijssen – 2019-05-02T16:02:06.2132So this is a challenge about iteratively updating a list of numbers? I'm confused what at all this has to do with threads. – xnor – 2019-05-02T17:25:39.317
@xnor If it was originally a job interview question, it probably would have been using threads to show the interviewee's experience. – mbomb007 – 2019-05-02T18:53:37.703
note: to all dear people who participate in this coding golf: the challenge will be ended in sunday. some bad things happened unexepectedly so i will not be able to fully monitor this. i will check this thread twice a day until sunday and then the winner will be decided. if you want to edit my post to implement leaderboard or cool features - feel free to do so and thank you very much for all of your support. it is always fun to see different approaches to a challenge and learn from them – Puzzle – 2019-05-03T06:03:59.960
@Puzzle I've updated my Java answer and added a program with threads. As I suspected, without threads is quite a bit shorter.. xD – Kevin Cruijssen – 2019-05-03T09:32:25.690