data-structures

Most optimal way to find the sum of 2 numbers represented as linked lists

Hello, I was trying to write a program for the problem I mentioned above, the numbers (i.e the lists) can be of unequal length, I was not able to figure out a way to do this other than the most commonly thought of approach i.e reverse list-1 reverse list-2 find the sum and store it in a new list represented by list-3 reverse the li...

Grep multi-layered iterable for strings that match (Python)

Say that we have a multilayered iterable with some strings at the "final" level, yes strings are iterable, but I think that you get my meaning: ['something', ('Diff', ('diff', 'udiff'), ('*.diff', '*.patch'), ('text/x-diff', 'text/x-patch')), ('Delphi', ('delphi', 'pas', 'pascal', 'objectpascal'), ('*.pas',), ('text/x-pascal',['lets',...

A C style string file format conundrum

I'm very confused with this wee little problem I have. I have a non-indexed file format header. (more specifically the ID3 header) Now, this header stores a string or rather three bytes for conformation that the data is actually an ID3 tag (TAG is the string btw.) Point is, now that this TAG in the file format is not null-terminated. So ...

Which data structure to add/look up/keep count of strings?

I'm trying to figure out what data structure to quickly support the following operations: Add a string (if it's not there, add it, if it is there, increment a counter for the word) Count a given string (look up by string and then read the counter) I'm debating between a hash table or a trie. From my understanding a hash table is fast...

When to choose RB tree, B-Tree or AVL tree?

As a programmer when should I consider using a RB tree, B- tree or an AVL tree? What are the key points that needs to be considered before deciding on the choice? Can someone please explain with a scenario for each tree structure why it is chosen over others with reference to the key points? ...

When to use a List over an Array in Java?

In Java, when would it be preferential to use a List rather than an Array? ...

How do you implement a Stack and a Queue in JavaScript?

What is the best way to implement a Stack and a Queue in JavaScript? I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures. ...

Distinguishing extra element from two arrays?

One of my friend was asked this question in an interview - You have given two integer arrays each of size 10. Both contains 9 equal elements (say 1 to 9) Only one element is different. How will you find the different element? What are different approaches you can take? One simple but lengthy approach would be - sort both arrays,...

Programming to interfaces and synchronized collections

This question relates to Java collections - specifically Hashtable and Vector - but may also apply elsewhere. I've read in many places how good it is to program to interfaces and I agree 100%. The ability to program to a List interface, for instance, without regard for the underlying implementation is most certainly helpful for decoupli...

Linked list interview question

This may be the old, but I couldn't think of an answer. Say, there are two lists of different lengths merge at one point; how do we know where the merging point is? We don't know the length and we should parse each list only once. ...

How to best maintain a database of average time-related values?

I want to store the average values of some data that is occasionally generated by users, which I then use in my application to predict future data. Now the problem I have is that this data may grossly change during the day - so for example users coming in at night time may generate much lower values then users coming in during the mornin...

Using a Python Dictionary as a Key (Non-nested)

Python doesn't allow dictionaries to be used as keys in other dictionaries. Is there a workaround for using non-nested dictionaries as keys? The general problem with more complicated non-hashable objects and my specific use case has been moved here. My original description of my use case was incorrect. ...

Implementing and indexing User Defined Fields in an SQL DB

I need to store a large table (several millions or rows) that contains a large number of user-defined fields (not known at compile time, but probably around 20 to 40 custom fields). It is very important (performance-wise) for me to be able to query the data based on those custom fields: i.e. "Select the rows where this attribute has that...

iterate vector, remove certain items as I go.

I have a std::vector m_vPaths; I will iterate this vector and call ::DeleteFile(strPath) as I go. If I successfully delete the file, I will remove it from the vector. My question is can I get around having to use two vectors? Is there different data structure that might be better suited for what I need to do? example: using iterator...

a layman's term for identifying relationship

There are couples of questions around asking for difference / explanation on identifying and non-identifying relationship in relationship database. My question is, can you think of a simpler term for these jargons? I understand that technical terms have to be specific and unambiguous though. But having an 'alternative name' might help ...

.NET - multi-threaded iteration over a single List (Of T) - What do I need to watch out for?

I'm using VB.NET. I want to build a large List (Of MyClassOrStructure) that becomes static after its initial population. I'm going to have multiple threads that need to iterate through this list to read data (no writing, inserting, or deleting) Are there any very bad things that I need to keep an eye out for? Thanks. ...

Best Java data structure to store a 3 column oracle table? 3 column array? or double map?

Hello :) What is the best data structure to store an oracle table that's about 140 rows by 3 columns. I was thinking about a multi dimensional array. By best I do not necessarily mean most efficient (but i'd be curious to know your opinions) since the program will run as a job with plenty of time to run but I do have some restrictions...

Is there an efficient index persistent data structure with multiple indexes

I am looking for an efficient indexed persistent data structure. I typically work in .NET and am aware of FSharp's Map however that implementation and most others I am aware of only provide a single 'index', the left side of the mapping. Basically here is the scenario public class MyObject public int Id { get; } public int Gro...

How do I swap elements in a List?

I am trying to learn about data structures and algorithms on my own. I wrote my own double-linked list in C and now I want to write some algorithms to perform on the list. What is the preferred way to swap list items? Is it better to swap the content or to rearrange the pointers which point to the next and previous list item? ...

.NET - What is the best/correct way to iterator through a List and remove misc. members?

In C++ using std::list, this is a simple matter of erasing what an iterator is pointing to (the erase statement returns the next valid member of the list). What's the best way to iterator through a list and remove members that match a certain criteria? ...