Concepts in C++

1. Difference between Map and Unordered_Map :

                |     map             | unordered_map
---------------------------------------------------------
Ordering        | increasing  order   | no ordering
                | (by default)        |

Implementation  | Self balancing BST  | Hash Table
                | like Red-Black Tree |  

search time     | log(n)              | O(1) -> Average 
                |                     | O(n) -> Worst Case

Insertion time  | log(n) + Rebalance  | Same as search
                      
Deletion time   | log(n) + Rebalance  | Same as search

2. setprecision() :

Setprecision when used along with ‘fixed’ provides precision to floating point numbers correct to decimal numbers mentioned in the brackets of the setprecison.

int main() 
{ 
    double pi = 3.14159, npi = -3.14159; 
    cout << fixed << setprecision(0) << pi <<" "<<npi<<endl; 
    cout << fixed << setprecision(1) << pi <<" "<<npi<<endl; 
    cout << fixed << setprecision(2) << pi <<" "<<npi<<endl; 
    cout << fixed << setprecision(3) << pi <<" "<<npi<<endl; 
    cout << fixed << setprecision(4) << pi <<" "<<npi<<endl; 
    cout << fixed << setprecision(5) << pi <<" "<<npi<<endl; 
    cout << fixed << setprecision(6) << pi <<" "<<npi<<endl; 
} 

Output:
3 -3
3.1 -3.1
3.14 -3.14
3.142 -3.142
3.1416 -3.1416
3.14159 -3.14159
3.141590 -3.141590

3. Value of INT_MAX and INT_MIN:

Values in a compiler where integers
are stored using 32 bits.

Value of INT_MAX is +2147483647.
Value of INT_MIN is -2147483648.

4. Difference Between BFS and DFS

S.NO

BFS

DFS

1.

BFS stands for Breadth First Search.

DFS stands for Depth First Search.

2.

BFS(Breadth First Search) uses Queue data structure for finding the shortest path.

DFS (Depth First Search) uses Stack data structure.

3.

The Time complexity of BFS is O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges.

The Time complexity of DFS is also O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges.

5. Difference between c and c++

C

C++

C does not support object oriented programming.

C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.

C does not support information hiding.

Data is hidden by the Encapsulation to ensure that data structures and operators are used as intended.

6. Can we access global variable if there is a local variable with same name?

we cannot access a global variable if we have a local variable with same name, but it is possible in C++ using scope resolution operator (::)

// C++ Implementation
#include <iostream>
 
using namespace std;
 
int x; // Global x
 
int main()
{
    int x = 10; // Local x
    cout << "Value of global x is " << ::x << endl;
    cout << "Value of local x is " << x;
    getchar();
    return 0;
}

Output
Value of global x is 0
Value of local x is 10

7. Static Keyword and Use of Static

Static keyword has different meanings when used with different types. We can use static keyword with:

Static Variables : Variables in a function, Variables in a class Static Members of Class : Class objects and Functions in a class

Static variables in a Function:

When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once and the value of variable in the previous call gets carried through the next function call.

Static variables in a class:

As the variables declared as static are initialized only once as they are allocated space in separate static storage so, the static variables in a class are shared by the objects. There can not be multiple copies of same static variables for different objects.

#include<iostream> 
using namespace std; 
  
class GfG 
{ 
   public: 
     static int i; 
      
     GfG() 
     { 
        // Do nothing 
     }; 
}; 
  
int main() 
{ 
  GfG obj1; 
  GfG obj2; 
  obj1.i =2; 
  obj2.i = 3; 
    
  // prints value of i 
  cout << obj1.i<<" "<<obj2.i;    
} 

This gives an error as we cannot create multiple copies for different object

Class objects as static:

Just like variables, objects also when declared as static have a scope till the lifetime of program. Consider the below program where the object is non-static.

// CPP program to illustrate 
// when not using static keyword 
#include<iostream> 
using namespace std; 
  
class GfG 
{ 
    int i; 
    public: 
        GfG() 
        { 
            i = 0; 
            cout << "Inside Constructor\n"; 
        } 
        ~GfG() 
        { 
            cout << "Inside Destructor\n"; 
        } 
}; 
  
int main() 
{ 
    int x = 0; 
    if (x==0) 
    { 
        GfG obj; 
    } 
    cout << "End of main\n"; 
} 

Output:

Inside Constructor
Inside Destructor
End of main

In the above program the object is declared inside the if block as non-static. So, the scope of variable is inside the if block only. So when the object is created the constructor is invoked and soon as the control of if block gets over the destructor is invoked as the scope of object is inside the if block only where it is declared. Let us now see the change in output if we declare the object as static.

// CPP program to illustrate 
// class objects as static 

#include<iostream> 
using namespace std; 
  
class GfG 
{ 
    int i = 0; 
      
    public: 
    GfG() 
    { 
        i = 0; 
        cout << "Inside Constructor\n"; 
    } 
      
    ~GfG() 
    { 
        cout << "Inside Destructor\n"; 
    } 
}; 
  
int main() 
{ 
    int x = 0; 
    if (x==0) 
    { 
        static GfG obj; 
    } 
    cout << "End of main\n"; 
} 

Output:

Inside Constructor
End of main
Inside Destructor

You can clearly see the change in output. Now the destructor is invoked after the end of main. This happened because the scope of static object is through out the life time of program.

Static functions in a class:

Just like the static data members or static variables inside the class, static member functions also does not depend on object of class. We are allowed to invoke a static member function using the object and the ‘.’ operator but it is recommended to invoke the static members using the class name and the scope resolution operator. Static member functions are allowed to access only the static data members or other static member functions, they can not access the non-static data members or member functions of the class.

// C++ program to demonstrate static 
// member function in a class 

#include<iostream> 
using namespace std; 
  
class GfG 
{ 
   public: 
      
    // static member function 
    static void printMsg() 
    { 
        cout<<"Welcome to GfG!"; 
    } 
}; 
  
// main function 
int main() 
{ 
    // invoking a static member function 
    GfG::printMsg(); 
} 

Output:

Welcome to GfG!

Main use of static keyword is memory management

8. Authentication vs. Authorization

What Is Authentication?

Authentication is the act of validating that users are whom they claim to be. This is the first step in any security process. Complete an authentication process through:

  • Passwords. Usernames and passwords are the most common authentication factors. If a user enters the correct data, the system assumes the identity is valid and grants access.

  • One-time pins. Grant access for only one session or transaction.

  • Authentication apps. Generate security codes via an outside party that grants access.

  • Biometrics. A user presents a fingerprint or eye scan to gain access to the system.

In some instances, systems require the successful verification of more than one factor before granting access. This multi-factor authentication (MFA) requirement is often deployed to increase security beyond what passwords alone can provide.

What Is Authorization?

Authorization in a system security is the process of giving the user permission to access a specific resource or function. This term is often used interchangeably with access control or client privilege.

In secure environments, authorization must always follow authentication. Users should first prove that their identities are genuine before an organization’s administrators grant them access to the requested resources

Authentication vs Authorization

9. What are namespaces and use of using namespace std

Namespaces allow us to group named entities. Namespaces are spaces which help us to resolve conflicts between same variable names.

std stands for standard namespace which contains declaration of various objects like cout, endl, string etc.

Syntax:- 

namespace namespace_name 
{
   int x, y; // code declarations where 
             // x and y are declared in 
             // namespace_name's scope
}


// Creating namespaces 
#include <iostream> 
using namespace std; 
namespace ns1 
{ 
    int value()    { return 5; } 
} 
namespace ns2  
{ 
    const double x = 100; 
    double value() {  return 2*x; } 
} 
  
int main() 
{ 
    // Access value function within ns1 
    cout << ns1::value() << '\n';  
  
    // Access value function within ns2 
    cout << ns2::value() << '\n';  
  
    // Access variable x directly 
    cout << ns2::x << '\n';        
  
    return 0; 
} 

10. Sorting algorithms time complexity

Algorithm

Time Complexity

Best

Average

Worst

Ω(n^2)

θ(n^2)

O(n^2)

Ω(n)

θ(n^2)

O(n^2)

Ω(n)

θ(n^2)

O(n^2)

Ω(n log(n))

θ(n log(n))

O(n log(n))

Ω(n log(n))

θ(n log(n))

O(n^2)

Ω(n log(n))

θ(n log(n))

O(n log(n))

Ω(n+k)

θ(n+k)

O(n^2)

Last updated