SAP ABAP Realtime (Scenario based) Interview Questions (11 - 20)

11. You need to remove duplicates from an internal table. What approach will you use?

The most common way is to sort the internal table by the key fields and then use DELETE ADJACENT DUPLICATES. This works well when duplicate records become adjacent after sorting. If I need guaranteed uniqueness from the beginning, I may use a hashed table with unique key.

SORT lt_data BY matnr werks.
DELETE ADJACENT DUPLICATES FROM lt_data COMPARING matnr werks.

12. How would you merge two internal tables based on a common key?

Usually, I read one table inside a loop of another table using the common key. For better performance, I sort the lookup table and use READ TABLE ... BINARY SEARCH, or I use a hashed table if the key is unique.

SORT lt_marc BY matnr.

LOOP AT lt_mara INTO DATA(ls_mara).
  READ TABLE lt_marc INTO DATA(ls_marc)
       WITH KEY matnr = ls_mara-matnr
       BINARY SEARCH.
  IF sy-subrc = 0.
    " merge logic
  ENDIF.
ENDLOOP.

In interview, you can say: “For repeated lookup, I always think about sorted or hashed tables.”

13. You need to sort and filter large internal tables efficiently. What techniques will you use?

I use SORT with the exact fields needed, and I avoid repeatedly sorting the same table inside loops. For filtering, I can use DELETE, LOOP ... WHERE, or build a new filtered internal table. In modern ABAP, expressions also help, but the main idea is to reduce unnecessary passes over large data.

In interview, you can say: “Sorting once and reusing the sorted table is better than sorting again and again.”

14. A program is consuming high memory due to large internal tables. How will you optimize it?

I first check whether all data is really needed at the same time. I may process data in chunks, free internal tables after use, clear work areas properly, and avoid copying large tables unnecessarily. Memory issues often come from keeping too much data in memory for too long.

FREE lt_big_data.
CLEAR ls_data.

You can also say: “I try to push filtering to the database so ABAP memory usage becomes smaller.”

15. You need to update internal table records conditionally. How will you do it?

I loop through the internal table and update only the records that match my condition. In modern ABAP, using field symbols is common because it updates the row directly without extra copying.

LOOP AT lt_data ASSIGNING FIELD-SYMBOL().
  IF -werks = '1000'.
    -status = 'A'.
  ENDIF.
ENDLOOP.

In interview, mention that field symbols are usually better for direct modification.

16. You want to read data faster from internal tables. Which table type will you choose and why?

It depends on the access pattern. I use a standard table for simple sequential processing, a sorted table when I need sorted access and binary search, and a hashed table when I want very fast key-based reads.

A strong answer is: “I choose the table type based on how often and how I read the data.”

17. You need to compare two internal tables and find missing records. How will you do it?

I would use a loop to iterate through one table and check if each record exists in the other table. If a record is not found, it's a missing record. For large data, I prefer sorted or hashed tables so the comparison stays efficient.

LOOP AT lt_tab1 INTO DATA(ls_tab1).
  READ TABLE lt_tab2 TRANSPORTING NO FIELDS
       WITH KEY matnr = ls_tab1-matnr.
  IF sy-subrc <> 0.
    WRITE: / 'Missing in table 2:', ls_tab1-matnr.
  ENDIF.
ENDLOOP.

In real projects, this logic is often used in reconciliation reports.

18. You are dealing with deep structures in internal tables. How will you handle them efficiently?

Deep structures contain strings, internal tables, or references, so I handle them carefully because copying them can be expensive. I prefer field symbols or references when possible, and I avoid unnecessary moves of large deep structures.

LOOP AT lt_deep ASSIGNING FIELD-SYMBOL().
  " work directly without copying
ENDLOOP.

In interview, you can say: “With deep structures, I try to reduce copies and work by reference.”

19. You need to group data from internal table and calculate totals. How will you implement it?

A simple approach is to sort the table by the grouping key and then calculate totals inside a loop. In modern ABAP, LOOP AT ... GROUP BY is also a nice way if available. The actual method depends on the ABAP version and project standards.

SORT lt_data BY werks.
LOOP AT lt_data INTO DATA(ls_data).
  AT NEW werks.
    CLEAR lv_total.
  ENDAT.

  lv_total = lv_total + ls_data-labst.

  AT END OF werks.
    WRITE: / ls_data-werks, lv_total.
  ENDAT.
ENDLOOP.

In interview, mention subtotal logic because that sounds practical.

20. You need to delete records from internal table based on multiple conditions. How will you handle it?

If the conditions are simple, I use DELETE ... WHERE. If the logic is more complex, I may loop and build a new result table instead of deleting line by line. For big tables, I keep the logic as simple as possible to avoid performance issues.

DELETE lt_data WHERE werks = '1000' AND labst = 0.