ProgrammingModerate
Which operation on a dynamic array of n elements has O(1) amortised cost when using doubling?
- A. Inserting at the front
- B. Appending one element at the end
- C. Deleting from the front
- D. Searching for the minimum
Correct answer: B. Appending one element at the end
Doubling spreads the copy cost over the appends, so each append is O(1) amortised. Front insertion moves every element each time.
More C Programming questions
- A loop does for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) work();. For n = 1024, how many times is work() called in total?
- Which of these is an O(n) method to test whether a string of length n is a palindrome in C?
- char s[] = "hello world"; char w = strchr(s, ' ') + 1; printf("%s", w); What is printed?
- char a[8] = "abc"; memset(a + 1, 'z', 2); printf("%s", a); What is printed?
- char s[] = "12abc"; char end; long v = strtol(s, &end, 10); printf("%c", end); What is printed?
- char buf[3]; int n = snprintf(buf, 3, "%s", "hello"); printf("%d", n); What is printed?