This post aims to incite you to share some algorithm challenges, so we can learn from those challenges, seek for answers for difficult problems we're facing, and hopefully apply their answers in case we encounter similar issues.
Let's start by sharing mine:
Challenge 1: Finding Array Elements
Intended difficulty level - 2(Little Javascript coding proficiency)
Situation - Consider the below piece of code:
It's given that:
- array is an Array
- num is a Number
- sign is always either 1 or -1
- Every element in array has a property num, which is a Number
Goal -
Returns null if array's empty
Returns the element in array with the smallest property num that's just larger than num if sign equals to 1.
Returns the element in array with the largest property num that's just smaller than num if sign equals to -1.
If there's more than 1 such elements, return any of them.
If there's no such elements, return the element in array with the smallest property num if sign equals to 1.
If there's no such elements, return the element in array with the largest property num if sign equals to -1.
No argument will be mutated.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new code in find_min_max_num_prop(although you can add anything you want).
Challenge 2: Eliminating Repeated Executions
Intended difficulty level - 3(Some Javascript coding proficiency)
Situation - Consider the below piece of code:
It's given that:
- All custom objects are prototypes of CustomObj.
- objs and active_objs are arrays of all custom objects and all active custom objects respectively.
- Everything's run by 1 single thread only(i.e.: no multithreading, concurrent computing, parallel programming, etc, so there's no need to worry about race conditions).
- When 1 special event starts, each active custom object will call do_something, but any other custom object(i.e., non active custom object) will only call do_something via do_something_once, which can only be called via do_something.
- do_something_cond will always return the same result during any such special event.
Goal - Ensures that no active custom object will call do_something more than once during any such special event, and each non active custom object will also call do_something exactly once during that special event if and only if do_something_cond returns true.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new codes in parts specified by do_something_once(although you can add anything whenever you want).
I'll continue to post more and more. Let's share yours, so we can all benefit from each other here.
Let's start by sharing mine:
Challenge 1: Finding Array Elements
Intended difficulty level - 2(Little Javascript coding proficiency)
Situation - Consider the below piece of code:
JavaScript:
/* Returns null if array's empty
Returns the element in array with the smallest property num that's just
larger than num if sign equals to 1
Returns the element in array with the largest property num that's just
smaller than num if sign equals to -1
If there's more than 1 such elements, return any of them
If there's no such elements, return the element in array with the smallest
property num if sign equals to 1
If there's no such elements, return the element in array with the largest
property num if sign equals to -1
No argument will be mutated */
function find_min_max_num_prop(array, num, sign) {
// Writes your codes here only
//
}; // find_min_max_num_prop
- array is an Array
- num is a Number
- sign is always either 1 or -1
- Every element in array has a property num, which is a Number
Goal -
Returns null if array's empty
Returns the element in array with the smallest property num that's just larger than num if sign equals to 1.
Returns the element in array with the largest property num that's just smaller than num if sign equals to -1.
If there's more than 1 such elements, return any of them.
If there's no such elements, return the element in array with the smallest property num if sign equals to 1.
If there's no such elements, return the element in array with the largest property num if sign equals to -1.
No argument will be mutated.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new code in find_min_max_num_prop(although you can add anything you want).
Challenge 2: Eliminating Repeated Executions
Intended difficulty level - 3(Some Javascript coding proficiency)
Situation - Consider the below piece of code:
JavaScript:
CustomObjManager = {
throw new Error("This is a static class");
}
//-------------------------------------------------------------------//
// Eliminates repeated do_something execution for all custom objects //
//-------------------------------------------------------------------//
// obj: The current custom object executing do_something
// objs: An array of all custom objects
// active_objs: An array of all active custom objects
CustomObjManager.do_something_once = function(obj, objs, active_objs) {
if this.do_something_cond() {
// Write your codes here only
//
}
} // CustomObjManager.do_something_once
// objs: An array of all custom objects
// active_objs: An array of all active custom objects
CustomObj.prototype.do_something = function(objs, active_objs) {
this.do_something_part_1();
CustomObjManager.do_something_once(this, objs, active_objs)
this.do_something_part_2();
} // CustomObj.prototype.do_something
- All custom objects are prototypes of CustomObj.
- objs and active_objs are arrays of all custom objects and all active custom objects respectively.
- Everything's run by 1 single thread only(i.e.: no multithreading, concurrent computing, parallel programming, etc, so there's no need to worry about race conditions).
- When 1 special event starts, each active custom object will call do_something, but any other custom object(i.e., non active custom object) will only call do_something via do_something_once, which can only be called via do_something.
- do_something_cond will always return the same result during any such special event.
Goal - Ensures that no active custom object will call do_something more than once during any such special event, and each non active custom object will also call do_something exactly once during that special event if and only if do_something_cond returns true.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new codes in parts specified by do_something_once(although you can add anything whenever you want).
I'll continue to post more and more. Let's share yours, so we can all benefit from each other here.
Last edited: