19

Recently I was asked this question on interview and I didn't know how to answer it.

Can anyone answer this question and describe it?

alex
  • 479,566
  • 201
  • 878
  • 984
Chvanikoff
  • 1,309
  • 3
  • 13
  • 21

2 Answers2

26

O(1) since the length is stored as an attribute: source

However, this trivia is worth countering with a discussion about micro-optimising theatre, as kindly provided by our hosts here and here; read those two links and you'll find a good talking point to change the momentum of the conversation next time similar questions come up, regardless of whether you know the particular answer!

How the interviewer reacts to your tangent will tell you a lot about how much you want to work with them..

Will
  • 73,905
  • 40
  • 169
  • 246
  • Thank you. Could you give some more similar articles to read? What about interviewer - he was looking for developer with much stronger qualification than I have and this question was not "just for fun". But it gave me better knowledge about my skill weaknesses. – Chvanikoff Mar 13 '11 at 21:01
  • 4
    +1 for "micro-optimization theater." Profile, [profile](http://www.xdebug.org/docs/profiler), [profile](http://mirror.facebook.net/facebook/xhprof/doc.html)! – Charles Mar 13 '11 at 21:12
  • The interviewer was likely reading a prepared list of questions? Even at Google. The sooner you take them off the track the better. – Will Mar 13 '11 at 21:59
  • 2
    I don't see where your source mentions that string length is stored as an attribute. – Nikolai Mar 09 '14 at 11:56
  • @Nikolai seems they have cleaned up the documentation in the last 3 years. However, you can also infer it from the fact that null characters do not terminate a string - `strlen( "te\0st" ) = 5` is mentioned in the comments. – Will Mar 09 '14 at 20:59
  • Whatever you were citing as a source appears to have been removed. – Quolonel Questions Dec 16 '15 at 17:30
  • 2
    Comparing one O(n) string concatenation implementation vs. some other O(n) string concatenation implementation is probably micro-optimization theater. Knowing whether `strlen` is O(1) or O(n) can have a huge impact; just look at how [`sscanf`'s unexpected O(n) implementation led to O(n^2) parsing](https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times-by-70/). – jamesdlin May 11 '21 at 06:14
  • It's probably best not to throw accusations of "micro-optimisation theatre" around if you don't actually *know* that it's theatre. Getting it right first time is much easier than maybe profiling later which very often [just doesn't happen](https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times-by-70/). – Timmmm Oct 04 '22 at 11:40
0

I would assume that function is O(n) because it would need to iterate through the string once.

grivescorbett
  • 1,605
  • 1
  • 21
  • 29
  • 1
    And why would it need to do that? You're assuming it's stored as an array of chars with no additional information. – mpen Aug 16 '12 at 22:00
  • Because that is the right answer, it will obviously not be quadratic and worse than linear. – Ferazhu Apr 11 '23 at 08:12