Is list::size() really O(n)?

In C++11 it is required that for any standard container the .size() operation must be complete in “constant” complexity (O(1)). (Table 96 — Container requirements). Previously in C++03 .size() should have constant complexity, but is not required (see Is std::string size() a O(1) operation?). The change in standard is introduced by n2923: Specifying the complexity … Read more

A Regex that will never be matched by anything

Leverage negative lookahead: >>> import re >>> x=r'(?!x)x’ >>> r=re.compile(x) >>> r.match(”) >>> r.match(‘x’) >>> r.match(‘y’) this RE is a contradiction in terms and therefore will never match anything. NOTE: In Python, re.match() implicitly adds a beginning-of-string anchor (\A) to the start of the regular expression. This anchor is important for performance: without it, the … Read more

What are the differences between NP, NP-Complete and NP-Hard?

I assume that you are looking for intuitive definitions, since the technical definitions require quite some time to understand. First of all, let’s remember a preliminary needed concept to understand those definitions. Decision problem: A problem with a yes or no answer. Now, let us define those complexity classes. P P is a complexity class … Read more

What guarantees are there on the run-time complexity (Big-O) of LINQ methods?

There are very, very few guarantees, but there are a few optimizations: Extension methods that use indexed access, such as ElementAt, Skip, Last or LastOrDefault, will check to see whether or not the underlying type implements IList<T>, so that you get O(1) access instead of O(N). The Count method checks for an ICollection implementation, so … Read more

es6 Map and Set complexity, v8 implementation

Is it a fair assumption that in v8 implementation retrieval / lookup is O(1)? Yes. V8 uses a variant of hash tables that generally have O(1) complexity for these operations. For details, you might want to have a look at https://codereview.chromium.org/220293002/ where OrderedHashTable is implemented based on https://wiki.mozilla.org/User:Jorend/Deterministic_hash_tables.