containers interview questions
Top containers frequently asked interview questions
I have a vector of myObjects in global scope.
I have a method which uses a std::vector<myObject>::const_iterator
to traverse the vector, and doing some comparisons to find a specific element.
Once I have found the required element, I want to be able to return a pointer to it (the vector exists in global scope).
If I return &iterator
, am I returning the address of the iterator or the address of what the iterator is pointing to?
Do I need to cast the const_iterator
back to a myObject, then return the address of that??
Source: (StackOverflow)
I have a JLabel in a Container.
The defaut size of the font is very small.
I would like that the text of the JLabel to take the maximum size.
How can I do that ?
Thanks :)
Source: (StackOverflow)
It is said that docker is not a VM and containers directly run on the host.
But why do containers need a base image OS ?
If process in containers run on the base image OS , what's the difference between a VM and docker(only the base image OS can be re-used and kept read-only?)
And why does it boot faster than a VM?
What makes up a base image OS ? kernel , rootfs or both?
Source: (StackOverflow)
There's a list of data structures having lazy and strict implementations:
Data.Map.Lazy
and Data.Map.Strict
Data.IntMap.Lazy
and Data.IntMap.Strict
Data.HashMap.Lazy
and Data.HashMap.Strict
Data.ByteString.Lazy
and Data.ByteString.Strict
Data.Text.Lazy
and Data.Text
What are the strengths and weaknesses of those implementations and what are the rules to follow when choosing a specific one?
Source: (StackOverflow)
What is the most suitable container just for strings holding in some array with non-predetermined upper boundary, which length is unknown on it's creation.
For simple code like:
var list = new SomeContainer(); // size is unknown
for()/foreach()/do()/while() // any loop
{
list.Add(string);
}
Is it StringCollection
as optimized Collection for string, or just Collection<string>
or List<string>
or ArrayList
? What is the different between them?
Source: (StackOverflow)
C++11 provides multiple ways to iterate over containers. For example:
Range-based loop
for(auto c : container) fun(c)
std::for_each
for_each(container.begin(),container.end(),fun)
However what is the recommended way to iterate over two (or more) containers of the same size to accomplish something like:
for(unsigned i = 0; i < containerA.size(); ++i) {
containerA[i] = containerB[i];
}
Source: (StackOverflow)
I am trying to build a heap with a custom sort predicate. Since the values going into it are of 'user-defined' type, I cannot modify their built-in comparison predicate.
Is there a way to do something like:
h = heapq.heapify([...], key=my_lt_pred)
h = heapq.heappush(h, key=my_lt_pred)
Or even better, I could wrap the heapq functions in my own container so I don't need to keep passing the predicate.
Source: (StackOverflow)
Working my way through Effective STL at the moment. Item 5 suggests that it's usually preferable to use range member functions to their single element counterparts. I currently wish to copy all the values in a map (i.e. - I don't need the keys) to a vector.
What is the cleanest way to do this?
Source: (StackOverflow)
Let's say I have some kind of collection and I obtained an iterator for the beginning of it. Now let's say I modified the collection. Can I still use the iterator safely, regardless of the type of the collection or the iterator?
To avoid confusion, here is the order of operations I talk about:
- Get an iterator of the collection.
- Modify the collection (obviously
not an element in it, but the collection itself).
- Use the iterator obtained at step 1. Is it stil valid according to STL standard?!
Source: (StackOverflow)
This is not exactly a technical question, since I know C kind of enough to do the things I need to (I mean, in terms of not 'letting the language get in your way'), so this question is basically a 'what direction to take' question.
Situation is: I am currently taking an advanced algorithms course, and for the sake of 'growing up as programmers', I am required to use pure C to implement the practical assignments (it works well: pretty much any small mistake you make actually forces you to understand completely what you're doing in order to fix it). In the course of implementing, I obviously run into the problem of having to implement the 'basic' data structures from the ground up: actually not only linked lists, but also stacks, trees, et cetera.
I am focusing on lists in this topic because it's typically a structure I end up using a lot in the program, either as a 'main' structure or as a 'helper' structure for other bigger ones (for example, a hash tree that resolves conflicts by using a linked list).
This requires that the list stores elements of lots of different types. I am assuming here as a premise that I don't want to re-code the list for every type. So, I can come up with these alternatives:
- Making a list of void pointers (kinda inelegant; harder to debug)
- Making only one list, but having a union as 'element type', containing all element types I will use in the program (easier to debug; wastes space if elements are not all the same size)
- Using a preprocessor macro to regenerate the code for every type, in the style of SGLIB, 'imitating' C++'s STL (creative solution; doesn't waste space; elements have the explicit type they actually are when they are returned; any change in list code can be really dramatic)
- Your idea/solution
To make the question clear: which one of the above is best?
PS: Since I am basically in an academic context, I am also very interested in the view of people working with pure C out there in the industry. I understand that most pure C programmers are in the embedded devices area, where I don't think this kind of problem I am facing is common. However, if anyone out there knows how it's done 'in the real world', I would be very interested in your opinion.
Source: (StackOverflow)
I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list.
The custom list defines an iterator;
class Iterator: public std::iterator<std::forward_iterator_tag, T> {
// ...
}
Iterator begin() {
return (Iterator(root));
}
Iterator end() {
return (Iterator(NULL));
}
with the appropriate operators overloaded.
Ideally, I would like to do this;
class Foo {
public:
Foo() {
std::list<int> x;
std::vector<int> y;
custom_list<int> z;
iter = x.begin(); // OR
iter = y.begin(); // OR
iter = z.begin();
// ...
};
private:
std::iterator<int> iter;
};
But obviously these are all iterators of different types. I can assume all the containers are of the same type however.
Is there an elegant way to solve this problem?
Source: (StackOverflow)
Previously, I used to use MFC collection classes such CArray
and CMap
. After a while I switched to STL containers and have been using them for a while. Although I find STL much better, I am unable to pin point the exact reasons for it. Some of the reasoning such as :
- It requires MFC: does not hold because other parts of my program uses MFC
- It is platform dependent: does not hold because I run my application only on windows.(No need for portability)
- It is defined in the C++ standard: OK, but MFC containers still work
The only reason I could come up is that I can use algorithms on the containers. Is there any other reason that I am missing here - what makes STL containers better than MFC containers?
Source: (StackOverflow)
I have the following structure in my application:
<div id="container">
<div id="child_container">
<div class="child"></div>
<div class="child"></div>
...
<div class="child"></div>
</div>
</div>
Each child div has a known fixed width, but the application allows more of them to be inserted in the child_container div.
What I'm trying to do is to have the container div expand horizontally when needed, given the total width of the child container.
This is what happens currently:
+------ container -------+
+--- child_container ----+
| child1 child2 child3 |
| child4 |
+------------------------+
If I set the child_container div width to a fixed value, I can get it to expand horizontally past the container div, which works despite being a bit ugly:
+------ container -------+
+------ child_container -+----+
| child1 child2 child3 child4 |
+------------------------+----+
However, that requires recalculating it whenever a new child is added.
Is there a way to do this without using fixed widths for child container, in a way such that the end result is
+--------- container ---------+
+------ child_container ------+
| child1 child2 child3 child4 |
+-----------------------------+
Thanks.
Source: (StackOverflow)
Scott Meyers's Item 18 says to avoid vector <bool>
as it's not an STL container and it doesn't really hold bools.
The following code:
vector <bool> v;
bool *pb =&v[0];
will not compile, violating requirement of STL containers.
Error:
cannot convert 'std::vector<bool>::reference* {aka std::_Bit_reference*}' to 'bool*' in initialization
vector<T>::operator []
return type is supposed to be T&, but why it's a special case for vector<bool>
?
What does vector<bool>
really consist of?
The Item further says:
deque<bool> v; // is a STL container and it really contains bools
Can this be used as an alternative to vector<bool>
?
Can anyone please explain this?
Source: (StackOverflow)