# Time Space Trade-Off of algorithms

Learn: In this article we are going to study about **what is time space tradeoff**? What is **need of time space trade-off**? How to **calculate time space trade-off**? How time space trade-off helps to calculate the efficiency of algorithm?

Submitted by Amit Shukla, on September 30, 2017

The best algorithm, hence best program to solve a given problem is one that requires less space in memory and takes less time to execute its instruction or to generate output. But in practice, it is not always possible to achieve both of these objectives. As said earlier, there may be more than one approaches to solve a same problem. One such approach may require more space but takes less time to complete its execution. Thus we may have to sacrifice one at the cost of the other. That is what we can say that there exists a time space trade off among algorithms.

Therefore, if space is our constraints then we have to choose a program that requires less space at the cost of more execution time. Other than that, if time is our constraint, then we have to choose a program that takes less time to complete its execution of statements at the cost of more space.

**In the analysis of algorithms, we are interested in the average case, the amount of time a program might be expected to take on typical input data and in the worst case the total time required by the program or the algorithm would take on the worst possible inputs of that algorithm**.

## Types of Trade-off

**Lookup tables Vs Recalculation**

An algorithm involving a lookup table is an implementation can include the entire table, which reduces computing time, but increases the amount of memory needed, or it can compute table entries as needed, increasing computing time, but reducing memory requirements.**Compressed Vs Un compressed data**

Problem of data storage can also be handling by using space and time tradeoff of algorithms. If data is stored is not compressed, it takes more space but access takes less time than if the data were stored compressed (since compressing the data reduces the amount of space it takes, but it takes time to run the decompression algorithm). It is depending upon the particular instance of the problem, either way is practical. There are also rare instances where it is possible to directly work with compressed data, such as in the case of compressed bitmap indices, where it is faster to work with compression than without compression.**Re Rendering Vs Stored images**

In this case storing only the SVG source of a vector image and rendering it as a bitmap image every time the page is requested would be trading time for space; more time used, but less space. Rendering the image when the page is changed and storing the rendered images would be trading space for time; more space used, but less time. This technique is more generally known as caching.**Smaller code Vs loop unrolling**

This technique is commonly used to makes the code longer for each iteration of a loop, but it saves the computation time required for jumping back to the beginning of the loop at the end of each iteration.Larger code size can be traded for higher program speed when applying loop unrolling.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.