last day (15 days later) » 

18:57
0
Q: Most efficient way to track values' position in a list

Fo.var list = ['a','b','c','d','e','f','g','h']; Each time I make an API call and get that list, showing it here as a JavaScript array, I would like to know if any of the items have moved quickly (defined as, say, more than x positions) in either direction over the past n requests. Each request I ...

Please read What is the X Y Problem, and then modify your question accordingly.
Fo.
Fo.
Done, let me know if I can clarify further
Still a bit vague. If this is about temporal ordering, why don't you just include a timestamp with each item?
Fo.
Fo.
I'm not the source of the data
Alright. Why do you need to know the ordering of the elements? Is there some way to sort them?
Fo.
Fo.
18:57
I want to be able to analyze the history of the requests and determine if one of the elements went from 10th to 2nd position over the last 20 requests for example
How do you identify a particular element for this analysis?
Fo.
Fo.
I don't need to know the ordering, I need to see the pattern of changes in the ordering
OK, but if you are to determine how much an element has moved, you'll need a way to identify that same element in the new array, correct?
Show me the JSON for a single element.
Fo.
Fo.
Hi Robert
I would determine it just by the key
ok example
[{
'a': 9
},{
'b': 0
},{
'c': 4
}]
the array will always contain objects containing the same keys
a, b, c
their values are irrelevant, I just want to know if a dropped or c rose quickly
So if I make requests every minute lets say, I'd like some kind of algorithm that can notify of rapid changes for any of the elements based on their key
I'm just struggling with how to store and analyze the data on each request.
I'm not even sure that Javascript arrays are guaranteed to have a particular ordering.
Fo.
Fo.
19:04
each element in the array has an index
in my example above a is at index 0, b at 1 and c at 2
so each request, those objects in the array will be at different indexes
how to track their changes over time and discover rapid changes
So what is the significance of the ordering?
What does "this element changed position from x to y" tell you about that element?
Fo.
Fo.
it could be anything, popularity of a product in a list for example
Alright. Well, do you know how to compare one array to another in this fashion?
Fo.
Fo.
yes, that's not the hard part
What is the hard part?
Fo.
Fo.
19:09
the difficult part is how do you store 1000 lists, and run a function on the list each time you add an instance of it, to say over the last 100 requests, show me the elements that have moved within a range of -10 to 10
or store 10,000 lists
Well, you only need to store the last 100, don't you?
Fo.
Fo.
and check for changes of -100 to 100 over the past 400 requests
The last 400 then.
Fo.
Fo.
the parameters could change
That's just an implementation detail.
Are you worried that it's 400, and not 4?
Fo.
Fo.
19:11
so I have lots of space for the DB, I store an unlimited number of arrays
Alright.
I get the feeling that your problem would be better served by a different solution, like a Temporal Database.
That's why I asked you to read about the X Y problem.
In any case, you've already solved the problem with arrays. You already told me that you know how to compare two arrays.
You just have to do that 400 times.
Fo.
Fo.
What I'm not understanding is how to track the changes in this array - I compare the 400 arrays one by one and get a result of a-1, b+7, c+3 etc
Tell me about the specific problem in the domain space you're trying to solve.
Fo.
Fo.
that tells me how many moves there were but not how quickly
Not about your proposed solution.
The real-life problem that you're bringing your proposed solution to bear on.
Fo.
Fo.
19:17
The problem is pretty generic
it was a theoretical question posed to me, and I'm trying to work out the solution
I assumed there would be specific approaches for problems like these that I could translate to python or js or whatever
An interview question?
Fo.
Fo.
you could say that :) - it's over now though
Figures.
Fo.
Fo.
I'm not quite sure what the sentiment behind that statement would be
Well, it's a hypothetical, highly theoretical question with no real-life counterpart that's designed, not to see if you have "the correct answer," but to see if you know how to solve problems.
I hate these kinds of questions, because they generally don't have any correlation to the kind of work you're actually going to be asked to do.
But you're still expected to solve them anyway.
Fo.
Fo.
19:24
I already gave the example of product popularity, but it could be used to detect movements in stock prices, or my wife's favourite nephew of the moment - you know, who is rising fast
I think it has lots of real life counterparts
Those kinds of data points are never dependent on their location in an array, though.
Fo.
Fo.
That's true about it being a difficult question, didn't get the sense they expected an answer, they just wanted to know how I worked through it
I suppose not
Rather, you'll have a collection of data points containing a timestamp and a stock price, which will yield to ordinary statistical analysis.
The correct, most practical answer is probably "Load these arrays into a more suitable data structure."
Fo.
Fo.
But again, the assumption is I don't have control over the data
It doesn't matter how you get the data. You can always load it up into some better data structure.
The original data format doesn't really matter, and the use of arrays is an artificial constraint.
19:30
(Just posted an answer)
Fo.
Fo.
Indeed
Hey YSharp, thank you I'll check it out
For what it's worth, GeeksForGeeks has several similar problems involving stock prices. All of them use arrays.
Fo.
Fo.
Interesting site
Thanks for talking through this with me
I'll check those out too
20:25
(Updated my answer, which wasn't taking all of your question's assumptions into account)

last day (15 days later) »