ProgrammingEasyAccenture · Cognizant pattern
What is the time complexity of get(i) on a Java ArrayList and on a LinkedList?
- A. O(1) and O(n)
- B. O(1) and O(1)
- C. O(log n) and O(n)
- D. O(n) and O(1)
Correct answer: A. O(1) and O(n)
ArrayList is backed by an array, so it jumps straight to index i. LinkedList must walk node by node from the nearer end, which is O(n).
More OOP with C++ and Java questions
- In C++, what does new do that malloc does not?
- What is printed? java class A { void show() { System.out.print("A"); } } class B extends A { void show() { super.show()…
- What is printed? cpp class Base { public: void show() { cout << "B1"; } virtual void vshow() { cout << "B2"; } }; class Derived : public…
- In C++, Base has a non-virtual destructor and Derived : public Base owns a heap buffer freed in ~Derived. What happens on: Base p = new…
- What is printed? java String a = "hi"; String b = "hi"; String c = new String("hi"); System.out.print((a == b) + " " + (a == c) + " " +…
- What is printed? java System.out.print('a' + 1); System.out.print(" "); System.out.print("" + 'a' + 1);