Open In App

What are Online and Offline query-based questions in Competitive Programming

Improve
Improve
Like Article
Like
Save
Share
Report

The query-based questions of competitive programming are mainly of two types:

  1. Offline Query.
  2. Online Query.
     

Offline Query

An offline algorithm allows us to manipulate the data to be queried before any answer is printed. This is usually only possible when the queries do not update the original element set before printing the result.

Features:

  1. Offline query offers a much wide range of use as compared to the Online query.
  2. They provide greater usability and opportunity for customizing the return data.
  3. The offline solution first read all queries and preprocess them (like sorting queries for making further process also efficient) then calculate and output the answer of each query in the initial given order.
  4. In an offline setting, all queries are presented in advance.
  5. Problems are easier to solve in an offline setting, as one may be able to reorder the queries or can work on multiple queries at the same time.
  6. Sorting queries and original data can help to achieve better efficiency of O(N + Q) instead of efficiency of O(N * Q) which is common in cases where sorting is not possible as queries keep streaming in as in the case of online queries.

One example of an Offline Query is the Segment Tree.

Example: 

Given an array A[] = {2,3,5,6,7,9} and the task is to answer q queries for this array.
Each query contains two integers l and r and requires to output the sum of the array from l to r.

Necessary Condition For Offline Query: 

This technique can be used only when the answer of one query does not depend on the answers of previous queries since after sorting the order of queries may change.

When to use Offline query:

  1. Offline queries should be used for large reports.
  2. When all the queries are known upfront.

Online Query

To answer the queries in order of their appearance and you can’t pre manipulate the queries for an efficient approach such queries are called online queries. 

Features:

  1. In such kinds of queries, our data may also get updated.
  2. The answer can not be precomputed in advance.
  3. The answer of one query also affects the answer of further queries. So, each query is answered one by one.
  4. Online queries will offer a response shortly after the query, thus allowing a quick resolution for the query.

Example:

Given an array A = {2,3,5,6,7,9} and the task is to answer q queries for this array.
Each query is of two types

  1. Update the ith index by A[i] = x 
  2. Find sum from l to r

In this example after each query, the data of the array is changing. So it can’t be solved by the offline query method.

Necessary Condition For Online Query:

Online queries can be used only when the data is changing and when sorting is not possible due to continuous streaming of queries. 

When to use Online Query:

  1. Online queries should be used when there is a need to query a few transactions at a specific date-time.
  2. They should be used when it is required to query small queries quickly.

Offline Query vs Online Query

Below are some of the differences between offline query and the online query:

S No. Offline Query Online Query
1 This allows to manipulation of the data to be queried before any answer is printed. The queries cannot be pre-manipulated and answer the queries in order of their appearance.
2 Offline queries are possible when the queries do not update the original element set before printing the result. In online queries, the data set may also get updated.
3 Offline queries provide better efficiency of O(N + Q), since sorting of queries and original data is done before computing the result. In the case of online queries, the queries keep on streaming due to which sorting of queries is not possible and efficiency goes to O(N * Q).
4 Offline queries should be used for large reports. Online queries should be used when it is required to query small queries quickly
5 In offline queries, all queries are present in advance. In online queries, the queries keep streaming.
6 In an offline query, the answer of one query does not depend on the answer of the previous query. In an online query, the answer of one query affects the answer of further queries.

Last Updated : 05 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads