Analysis : Codechef Starters 99 Division 3

  

 Starters 99 Division 3 (Rated)

 

Mindlessly strolling through the question set while watching THIS movie. Its getting cold in haldwani. Possibly the Starters 99 was a fun distraction. The movie was good too. 😍😎

 1. Endless Appetizers

Formula = ceil((LB_sticks + extra_sticks)/num_of_sticks_in_singlePlate) 

 2. Card Swipe

This was pretty simple ones. Taking into account the insertion of duplicate elements could easily solve this. For this, using a set would be the best approach.
 
for i in data:
    if i not in checker_set :
        checker_set.add(i)
    else:
        checker_set.remove(i)
       
    max_so_far = len(checker_set)
    if max_so_far > answer:    
        answer = max_so_far

 3. Exclusion-Inclusion

Simple but tricky one. The only trick is to take the sum of the array and sort it. Once you have done that, keep subtracting the elements from array with the sum and print the answer.

 4. Segment Three

Possibly the most challenging question of the contest. It was a Dynamic programming problem.

The problem can be simply divided into 9 different finite states, which is simply following state
(0,0), (0,1), (1,0), (1,1), (2, 0), (0, 2), (2,1), (1,2), (2,2). 

For each state we basically check whether adding that state (a, b) to (A[0],A[1]) of the Array will make it divisible by 3. Taking the (a+A[0])%3 say A_2 and (b+A[1])%3 say A_1 of first 2 elements as anchor point, we loop through the array and select the Ai element and check minimum number of additions required to make changes in A2 such that (A0, A1, A2) gets divisible by 3. Similarly for (A1, A2, A3), (A2, A3, A4).

Repeat this for remaining 8 states. The State with the minimum number of changes is the answer. 

 

for a, b in ((0,0), (0,1), (1,0), (1,1), (2, 0), (0, 2), (2,1), (1,2), (2,2)):
    suma = a + b
    A_2, A_1 = (A[0] + a)%3, (A[1] + b)%3
    for c in A[2:]:
       inc = (3 - (A_2 + A_1 + c)%3)%3
       suma += inc
       A_2, A_1 = A_1, (c + inc)%3
       ans = min(ans, suma)


Hope you had fun reading the blog. Thank you. Have a great one. 💓

 

Techniques to Boost Your API Performance

 

 

 
 
 Are you tired of slow api making your application sluggish ? You are in right place. Today i am going to share with you 7 techniques that will help you boost your API performance. But before really jumping into optimization, you should ask yourself whether it really has performance issues.
 

1. Caching : You store expensive computations so that you don't have to repeat the calculations. If you have an endpoint which is frequently accessed, so request params, you can avoid repeated database hits by caching the response in redis or memcache.

from flask_caching import Cache

@app.route('/data')
@cache.cached(timeout=60) # Cache the result for 60 seconds
def get_data():
  # Expensive operation or database query to retrieve data
  data = retrieve_data_from_database()
  return data

2. Connection Pool :  Instead of Creating new connection for each request made to the server,  

3. Avoid N+1 Query pattern :

4.
Pagination : Break Response into smaller manageable pages using limit or offset paramater. This will reduce the weight on client side.

5.
JSON Serialization : Speed of Serialization while returning a JSON can make a huge difference in your API performance. Less the time required to convert data into JSON format.

6. Compression : By enabling compression on large API payload, we can reduce the amount of data transfer  over the network. The client then decompresses the data. Efficient algorithms like Brotli can help to achieve this. Also CDNs like cloudflare can handle compression for you.

7. Asynchronous logging : In many Apps, the time taken to write logs is negligible. However in high throughput systems, where every milliseconds counts, the time taken to write logs can add up. Using Async Logging, we can reduce the time of writes. However if the application crashes during the time of writes, you might lose some amount of data.

I hope this small tutorial was helpful to you. Please feel free to share your ideas and comments on this article. Your comments not only help us engage with you but also improve our quality standards. We strongly believe in setting the benchmarks. Thank you for your time. 😉

4 Most Popular API Architecture Styles

API (Application Programming Interface) architecture styles define the way APIs are designed and structured, determining how clients interact with the API and how data is exchanged.

     
     
    There are several popular API architecture styles: 

  1. REST (Representational State Transfer): REST is one of the most widely used API architectures. It uses standard HTTP methods (GET, POST, PUT, DELETE) to perform operations on resources represented by URLs. RESTful APIs are stateless, meaning each request from a client to the server must contain all the information needed to understand and process the request.

  2. GraphQL: GraphQL is a query language and runtime for APIs developed by Facebook. Unlike REST, where clients get a fixed set of data from predefined endpoints, GraphQL allows clients to request exactly the data they need, making it more flexible and efficient for fetching data from the server.

  3. SOAP (Simple Object Access Protocol): SOAP is an older API architecture that relies on XML messages to communicate between client and server. It defines a strict set of rules for structuring requests and responses and is often used in enterprise-level applications where a standardized communication protocol is required. They are mostly used in Payment Gateways and Financial Services

  4. gRPC: gRPC is a high-performance, open-source RPC (Remote Procedure Call) framework developed by Google. It uses protocol buffers (protobuf) as its Interface Definition Language (IDL) to define the API and supports both unary and streaming calls, making it suitable for real-time applications.

    I hope you found this insightful. Please feel free to share your comments.
    Keep reading. Keep growing. Thank you !!

Project Ideas for Python Development.


Python is a versatile and widely used programming language in the field of programming. Having projects indicates your knowledge and expertise in implementing and managing Python ecosystem. Python can be used effectively in various aspects of IT, including infrastructure automation, configuration management, deployment automation, and scripting. Here are the project Ideas for Python Development.

  1. Task Manager: Create a command-line or GUI application that allows users to manage their tasks, set deadlines, prioritize tasks, and track their progress.

  2. Weather Application: Build a weather app that retrieves weather data from an API and displays current weather conditions, forecasts, and alerts for a given location.

  3. Expense Tracker: Develop an application that helps users track their expenses, categorize them, and generate reports or visualizations to analyze their spending habits.

  4. Quiz Game: Design a quiz game where users can answer multiple-choice questions on various topics. Keep track of scores and provide feedback on correct and incorrect answers.

  5. Recipe Finder: Create a program that allows users to search for recipes based on ingredients they have on hand. The application can retrieve recipes from an API and provide suggestions.

  6. File Organizer: Build a script that automatically organizes files in a specified directory based on file type, creating folders and moving files accordingly.

  7. URL Shortener: Develop a URL shortening service similar to bit.ly, where long URLs can be shortened and stored in a database. Users can then access the original URL by using the shortened version.

  8. Chatbot: Build a chatbot using Natural Language Processing (NLP) libraries. The chatbot can provide information, answer questions, or perform specific tasks based on user input.

  9. Social Media Analyzer: Create a program that fetches data from social media APIs (e.g., Twitter, Instagram) and analyzes trends, user interactions, or sentiment analysis based on posts or hashtags.

  10. Password Manager: Develop a secure password manager that stores encrypted passwords for various accounts and allows users to generate strong passwords and easily retrieve them when needed.

Remember to choose a project that aligns with your interests and skill level. Additionally, consider incorporating additional features or functionalities to make the project more challenging and impactful. Happy coding!