Algorithms interview questions


Q10: What is A*, what are its implementation details, and what are its advantages and drawbacks concerning traversing graphs towards a target?

Ans: A* is a path-screening method that attempts just to discover solutions without going into irrelevant sections of the network fast and without the attempt to locate them quickly.

It achieves this via a heuristic approach that approximates the distance between the node and the target node. This is explained most trivially in a picture that shows a space mesh. If we aim to identify a path between A and B, we may specify the heuristic distance from Euclidean point to B, scaled up by the factor selected.

That heuristic is used by adding it from our starting point to our distance. Furthermore, the remainder of the application is the same as Dijkstra. The primary advantage of the algorithm is the fast average speed. The primary drawback is that it does not locate ideal answers but any heuristic option.

Q11: What is a linear search?

Ans: On a group of elements, linear search is employed. It relies on the approach to cross an overview of the characteristics of all items encountered on the path from start to finish.

Suppose, for instance, an array of integer items. The location of all items with their value should be found and printed. Here, a linear search operates in a flux that matches every element with the number from the beginning of the list to the end of the list, and if the condition is 'Actual, then publishing the element's position.'

Implementing Linear Search

The linear search must be carried out in the following phases.

Step1: Using the loop, cross the array
Step2: Compare the goal value in each iteration with the current array value
Step3: Returns the current array index if values match.
Step4: Shift to the next array element if the values do not match.
Step5: If there is no match, return -1

Q12: You have a set of date intervals represented by StartDate and EndDate. How can the longer time period spanned by them be calculated efficiently? What's the complexity of the time?

Ans:
FUNCTION MaxTimespan(StartDates, EndDates) Sort(StartDates, EndDates) ActStartDate = StartDates[1] ActEndDate = EndDates[1] ActTimespan = ActEndDate - ActStartDate FOR i in 2..StartDates.Length IF StartDates[i] BETWEEN ActStartDate AND ActEndDate ActEndDate = MAX(ActEndDate, EndDates[i]) ActTimespan = MAX(ActTimespan, ActEndDate - ActStartDate) ELSE ActStartDate = StartDates[i] ActEndDate = EndDates[i] END IF END FOR RETURN ActTimespan

The overall time complexity is O(NlogN) because:

  • Sort intervals by start date. Time complexity is O(NlogN).
  • As the current range, take the first interval. Increase the Enddate of the current range if necessary and extend the maximum time if needed if the current StartDate is in the real range. Use the current interval otherwise as a new current range. The complexity of time is time (N).

Q13: Explain what is the difference between the best-case scenario and the worst-case scenario of an algorithm?

Ans:
  • Best case scenario: An algorithm's best-case scenario is described as the arrangement of data for the algorithm. For instance, we perform a binary search for which, in the exact center of the data you are looking for, the preferred situation would be. The best time complexity is 0 (1)
  • Worst case scenario:  The worst input set for a certain algorithm is referred to. For instance, fast-sort, which might work harder if you choose for the pivot value the greatest or minor entry in a sublist. It will degrade quickly to O (n2).

Q14: Write an algorithm to insert a node in the Binary search tree?

Ans: The operation of the inserted node is seamless. The root node has to be compared and the value of the node to be added must be compared left (if smaller) or right (if larger).

Algorithm:

Make a root node as an existing. If the < root o is inserted node, then cross left. If the node is left, insert the node here. If the node is inserted> root. If the child is right, pass right. If the child is not right, insert a node in this case.

Q15: What is the difference between the Singly Linked List and Doubly Linked List data structure?

Ans: This is a typical data structure interview. The main distinction is the ability to navigate between the individually linked list and the doubly linked list.

You cannot go back via a single linked list since there's only a node pointing to the next node and no point to the prior node.

The doubly linked list, on the other hand, allows you to browse any linked list in both directions as two points are kept to the first and second nodes.